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



  • 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?



  • QPainter requires QGuiApplication, and QGuiApplication ultimately links you to X11 through libQt5Gui.so.5

    One way I can see you getting this to fly is to fake an X server using "Xvfb":http://www.wikipedia.org/wiki/Xvfb Clearly this will not work if there is no X at all on the target machine.

    There is an "offscreen" platform driver that might work. Start your app with "-platform offscreen". Edit: Scratch this, it requires X11 libraries also.



  • I tried "-platform offscreen", but I got the error:

    QFontDatabase: Cannot find font directory '/***/lib/fonts' - is Qt installed correctly?



  • I had the same problem trying to run my QTestLib QGuiApplication unit tests on a headless build system. I worked around it by providing a X virtual server.

    Xvfb :1 &
    DISPLAY=:1 make check
    pkill Xvfb

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.