简体   繁体   English

如何在无头模式下运行 Qt 应用程序(不显示我的 GUI)

[英]How to run a Qt application in headless mode (without showing my GUI)

I have a QT application based on a QApplication and supposing that my application has a complex GUI ( QDialog , QMainWindow ...).我有一个基于QApplication的 QT 应用程序,并假设我的应用程序具有复杂的 GUI( QDialogQMainWindow ...)。

My Application can run in two modes:我的应用程序可以以两种模式运行:

  • with GUI带图形用户界面
  • in headless mode在无头模式下

I would like to know how I can launch the application in headless mode (that is to say without GUI visible)我想知道如何以无头模式启动应用程序(也就是说没有 GUI 可见)

From a very basic code, below, what argument shall I have to allow this?从下面的一个非常基本的代码,我应该有什么论据来允许这个?

int main(int argc, char*argv[])
{
  QApplication app(argc, argv);
  // which option should I add to argv to run in headless mode
  return app.exec();
}

There are several options here.这里有几个选项。 Either you need a Qt Console Application or you need a headless GUI Application.您要么需要 Qt 控制台应用程序,要么需要无头 GUI 应用程序。

You will find truly running a GUI in headless mode rather tricky.你会发现真正在无头模式下运行 GUI 相当棘手。 This applies in case you need to run the very same app in a Linux system which does not have the installed GUI libraries, like a minimal setup.这适用于您需要在没有安装 GUI 库的 Linux 系统中运行相同应用程序的情况,例如最小设置。 Without extensive xorg and/or EGL libraries you'll find it impossible.如果没有广泛的 xorg 和/或 EGL 库,您会发现这是不可能的。

But fear not, you can do it with minimal impact by using either the Qt VNC platform plugin or with with the help of Xvfb .但不要害怕,您可以通过使用 Qt VNC 平台插件或在Xvfb的帮助下以最小的影响做到这一点。 So in short所以简而言之

Solution 1: Hide it with Qt's VNC plugin解决方案 1:使用 Qt 的 VNC 插件隐藏它

$ QT_QPA_PLATFORM="vnc"  ./my-app

it's the same as

$ ./my-app -platform vnc

You'll find that you software has a GUI but it's running in headless mode, in order to view the GUI you just connect to it with any vncviewer.你会发现你的软件有一个 GUI,但它在无头模式下运行,为了查看你只需使用任何 vncviewer 连接到它的 GUI。

Solution 2: Avoid dependencies with Qt's VNC plugin解决方案 2:避免依赖 Qt 的 VNC 插件

The same as the other solution, and you can just hide your GUI by not showing it.与其他解决方案相同,您可以通过不显示 GUI 来隐藏它。

Solution 3: Nullify render with offscreen render解决方案 3:使用屏幕外渲染来取消渲染

This is rather similar to VNC but you'll get a totally null output, no way for GUI interaction:这与 VNC 非常相似,但你会得到一个完全 null output,无法进行 GUI 交互:

$ ./my-app -platform offscreen

Solution 4: Run Xvfb and launch it there解决方案 4:运行 Xvfb 并在那里启动它

You can run a fake Xorg server and run things over there.你可以运行一个假的 Xorg 服务器并在那里运行东西。

export DISPLAY=:1
Xvfb :1 -screen 0 1024x768x16 &
./myapp &

From the given solutions I'd prefer the offscreen render, but your Qt compilation might not have the plugin or it might ask for xcb or egl libraries.从给定的解决方案中,我更喜欢屏幕外渲染,但是您的 Qt 编译可能没有插件,或者它可能需要 xcb 或 egl 库。 It's your choice.这是你的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM