Create a truly headless QApplication instance

可紊 提交于 2021-01-04 05:34:25

问题


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?


回答1:


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] = "app_exe";
   argument[1] = "-platform";
   argument[2] = "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.



来源:https://stackoverflow.com/questions/42686691/create-a-truly-headless-qapplication-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!