创建一个真正无头的 QApplication 实例 [英] Create a truly headless QApplication instance

查看:41
本文介绍了创建一个真正无头的 QApplication 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Qt 5.8(通过PyQt5)应用程序,其许多测试都需要一个实时的 QApplication 实例才能测试gui小部件的交互作用.

但是,在我的新Mac OS X(10.11.6)计算机上运行这些测试时,会不断创建和销毁窗口,从而导致整个系统的UI变得高度不稳定(尽管测试通过了预期的结果).

我的测试甚至不调用 QApplication.exec()- QApplication()构造函数足以导致此问题.在我的linux机器上,这没有问题,因为没有创建窗口(至少在我的X配置下).

QCoreApplication 不是一个选项,因为需要创建和使用实际的GUI小部件.尝试将两者互换会导致大量测试失败.

Qt 5.8中是否有一种方法可以创建一个真正的无头 QApplication 实例,而无需使用 QCoreApplication ?

解决方案

我遇到了同样的问题,我认为解决方案是将 -platform offscreen 参数添加到 QApplication .

我在此处

假设您的应用程序可执行文件名为 app_exe

  int main(int argc,char * argv []){int arguments_count = 3;char *参数[3];arguments [0] = const_cast< char *>("app_exe");参数[1] = const_cast(-platform");参数[2] = const_cast< char *>(屏幕外");QApplication app(argument_count,argument);返回app.exec();} 

因此,您应该运行您的应用程序,但不要显示任何GUI.要查看应用程序是否正在运行,您应列出所有正在运行的任务,并且您的应用程序应在其中.

I have a Qt 5.8 (via PyQt5) application whose many tests require a live QApplication instance in order to test gui widget interactions.

However on my new Mac OS X (10.11.6) machine when running these tests windows are constantly being created and destroyed, causing the entire system's UI to become highly unstable (although the tests pass as expected).

My tests do not even call QApplication.exec() - the QApplication() constructor is enough to cause this. On my linux machine this is no issue, as windows are not created (at least under my X configuration).

QCoreApplication is not an option as actual GUI widgets need to be created and worked with. Attempts to swap the two cause massive test failures.

Is there a way in Qt 5.8 to create a truly headless QApplication instance without using QCoreApplication?

解决方案

I had the same problem and I think the solution is to add the argument, -platform offscreen to the command line of the QApplication.

I have found the information here

Suppose your application executable is named app_exe

int main(int argc, char* argv[])
{
   int argument_count = 3;
   char* argument[3];
   argument[0] = const_cast<char*>("app_exe");
   argument[1] = const_cast<char*>("-platform");
   argument[2] = const_cast<char*>("offscreen");
   QApplication app(argument_count, argument);
   return app.exec();
}

As a result, you should have your application running but without showing any GUI. To see if the application is running, you shall list all running tasks and your application shall be within them.

这篇关于创建一个真正无头的 QApplication 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆