Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt 5.1 QApplication Without Display - QXcbConnection: Could not connect to display

Tags:

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?

like image 473
Vincent Avatar asked Jul 31 '13 19:07

Vincent


1 Answers

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.

like image 92
peppe Avatar answered Oct 29 '22 21:10

peppe