Linux non Gui Application drawImage crash

  • When I make a drawImage in a non gui linux application on a QPainter on QSvgGenerator I get "Segmentation fault". Is this the normal behaviour ? Drawing of QPainterPaths or QRects works fine.

  • I think "yes". Image processing files are stored in src/gui/images sub-directory in the Qt source tree.
    You should use QApplication class instead of QCoreApplication. And link with QtGui library.
    This should help.

  • That does mean you cannot run your application without a running X session.

  • The problem is i want an application which does not need a X server. And think it has nothing to do with image processing, because there is no problem in drawing a QImage on QImages or QPrinters. Only when i try to draw a QImage on a QSvgGenerator it crashes.

  • "QSvgGenerator": inherits from "QPaintDevice":

    bq. From the QPaintDevice docs:
    Warning: Qt requires that a QApplication object exists before any paint devices can be created. Paint devices access window system resources, and these resources are not initialized before an application object is created

    But it seems as if you have a graphics system initialized already. Could you give us a simple working example that demonstrates the bug (C++ sources for a main.cpp and a .pro).

  • I have a QApplication. Here is some sample code:
    @QT += core
    QT += svg

    TARGET = svgexperiment
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp@

    @#include <QtCore>
    #include <QtGui>
    #include <QtSvg>

    int main(int argc, char *argv[])
    // initialize application
    // if I set the gui parameter to true it works without problems
    QApplication a(argc, argv, false);

    // create an image
    QImage img(300, 200, QImage::Format_ARGB32_Premultiplied);

    // draw some stuff on the image
    QPainter p(&img);

    // copy img and overwrite the copies content
    QImage sndImg(img);
    p.drawImage(0,0, sndImg); // write image on image to show that this works

    // save this image for control reasons"/home/henrik/img_2.png");

    // create a svg generator
    QSvgGenerator generator;
    generator.setViewBox(QRect(QPoint(0,0), generator.size()));

    // draw on the generator
    p.fillRect(0,0, 150,100, Qt::yellow);
    p.drawImage(0,0, img); // this line crashes if it's a non gui application

    return 0;


  • What do you exactly mean when you say "gui" or "non-gui"?

    My understanding is that "gui" uses QApplication, and "non-gui" uses QCoreApplication.

  • bq. QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )
    Constructs an application object with argc command line arguments in argv. If GUIenabled is true, a GUI application is constructed, otherwise a non-GUI (console) application is created.

    I mean this third QApplication parameter.

  • I found out that drawImage in QSvgGenerators PaintEngine want's to convert the QImage to a QPixmap and this doesn't work if the application isn't connected to a X server.
    I don't understand why this is neccessary, image data is just saved as PNG data int the svg file and this is normally done with QImage and not QPixmap.
    Can someone tell me why this is done via QPixmap ?

  • [quote author="Henrik" date="1291188405"]Can someone tell me why this is done via QPixmap ?[/quote]

    If the Qt code is clear in that place maybe the simplest way is to patch the Qt library? Sorry for stupid suggestion :)

  • For some reasons there might be some transformations on the image (e.g. to scale it to the bounding rect or some transformation matrix). This is done using pixmaps. Even calling the paintEngine() pointer of QSvgGenerator directly does not help (this does not use a QPixmap). I don't know why this is not called correctly. Maybe you should open a bug at

  • @
    #0 0xb76d342d in QX11Info::appDepth(int) () from /usr/lib/
    #1 0xb76d35e4 in QX11Info::depth() const () from /usr/lib/
    #2 0xb7725c70 in QX11PixmapData::fromImage(QImage const&, QFlagsQt::ImageConversionFlag) ()
    from /usr/lib/
    #3 0xb770fd0c in QPixmap::fromImage(QImage const&, QFlagsQt::ImageConversionFlag) () from /usr/lib/
    #4 0xb7776d5e in QPaintEngine::drawImage(QRectF const&, QImage const&, QRectF const&, QFlagsQt::ImageConversionFlag) () from /usr/lib/
    #5 0xb7790e3b in QPainter::drawImage(QPointF const&, QImage const&) () from /usr/lib/
    #6 0x080499e6 in QPainter::drawImage(int, int, QImage const&, int, int, int, int, QFlagsQt::ImageConversionFlag) ()
    #7 0x08049575 in main ()

    int QX11Info::appDepth(int screen)
    return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].depth : 32;

    :( Have no time to dig deeper right now, but I have to say that I got similar situation when trying to convert html to pdf by Qt printer. It can be done only in QApplication with X11 or qws access.

  • Is X11 should be running or just libraries should be installed?

  • [quote author="blex" date="1291440121"]Is X11 should be running or just libraries should be installed?[/quote]

    X11 must be running and you must have set your DISPLAY environment variable. If you unset DISPLAY the X11 GUI cannot be initialized and the program fails to start.

Log in to reply

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