问题
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