20
votes

I'm using Qt5.1 and I'm trying to create a QApplication without a display. I need to draw text with QPainter, so I need to use QApplication (or QGuiApplication), otherwise I get a segfault.

The application worked fine in Qt4.8, but fails in Qt5.1 on a headless version of Ubuntu with the error:

"QXcbConnection: Could not connect to display".

In Qt 4.8, I was able to use the following constructor with GUIenabled = false to create a QApplication that did not require a display:

QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )

In Qt5.1, the constructor for QApplication no longer has the GUIenabled flag.

I scanned the source code briefly, and there does seem to be a flag in the QApplication constructor, but it is undocumented as to what options can be used in that flag. Using "false" does not work.

How can I create a QApplication without a display? Is there an alternative method to telling QApplication GUIenabled = false? Alternatively, can I create a QCoreApplication that will not segfault when drawing text with QPainter on a QImage?

2
I ran into this problem after deleting file(s) in /tmp .Geremia

2 Answers

22
votes

Yes, that's a Qt 3 (?) thing that is gone in Qt 5. Try running your application with the -platform offscreen command line option instead.

Note that you don't need QApplication or linking to QtWidgets to just draw upon a QImage, using QGuiApplication (and linking to QtGui) is sufficient.

4
votes

If you want to create an app without GUI, you need to use QCoreApplication instead of QApplication.