The Linux Page

QtTest and "Cannot create a QWidget when no GUI is being used"

Problem

I have to use Qt these days. It's a good system, but there are a few tricks to know about.

As I was starting to write a test, the test needed a lot of numbers but nothing much otherwise. So I decided to limit the #include to a minimum by deleting all of those that I did not need.

The result was this error:

"Cannot create a QWidget when no GUI is being used"

I looked into it and sure enough people talking about it say: Well... where is your GUI?

Great, but I don't see how that helps me in fixing the problem, does it do it for you? Neither?!

Solution

Well... in my deletion of all the #include, I removed this one:

#include <QApplication>

And that include means business: you start using the GUI as expected.

When no GUI related include is #include'd, then the GUI is not turned on so the QTEST_MAIN() function creates QCoreApplication, instead of the expected (by most) QApplication object. Including any file that means GUI forces that QApplication and then you don't get the error.