November 30, 2010

Henrik Henrik
Lab Rat
30 posts

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.

13 replies

November 30, 2010

blex blex
Lab Rat
191 posts

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.

 Signature 

————————————

Oleksiy Balabay

November 30, 2010

Franzk Franzk
Lab Rat
830 posts

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

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

November 30, 2010

Henrik Henrik
Lab Rat
30 posts

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.

November 30, 2010

Volker Volker
Robot Herder
5428 posts

QSvgGenerator [doc.qt.nokia.com] inherits from QPaintDevice [doc.qt.nokia.com].

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).

December 1, 2010

Henrik Henrik
Lab Rat
30 posts

I have a QApplication. Here is some sample code:

svgexperiment.pro

  1. QT       += core
  2. QT       += svg
  3.  
  4. TARGET = svgexperiment
  5. CONFIG   += console
  6. CONFIG   -= app_bundle
  7.  
  8. TEMPLATE = app
  9.  
  10.  
  11. SOURCES += main.cpp

main.cpp

  1. #include <QtCore>
  2. #include <QtGui>
  3. #include <QtSvg>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.  // initialize application
  8.  // if I set the gui parameter to true it works without problems
  9.  QApplication a(argc, argv, false);
  10.  
  11.  
  12.  // create an image
  13.  QImage img(300, 200, QImage::Format_ARGB32_Premultiplied);
  14.  
  15.  // draw some stuff on the image
  16.  QPainter p(&img);
  17.  p.setPen(Qt::blue);
  18.  p.setBrush(Qt::green);
  19.  p.drawEllipse(img.rect());
  20.  p.end();
  21.  img.save("/home/henrik/img_1.png");
  22.  
  23.  // copy img and overwrite the copies content
  24.  QImage sndImg(img);
  25.  sndImg.fill(QColor(Qt::red).rgba());
  26.  p.begin(&img);
  27.  p.drawImage(0,0, sndImg); // write image on image to show that this works
  28.  p.end();
  29.  
  30.  // save this image for control reasons
  31.  img.save("/home/henrik/img_2.png");
  32.  
  33.  
  34.  // create a svg generator
  35.  QSvgGenerator generator;
  36.  generator.setFileName("/home/henrik/generated.svg");
  37.  generator.setSize(QSize(300,200));
  38.  generator.setResolution(300);
  39.  generator.setViewBox(QRect(QPoint(0,0), generator.size()));
  40.  
  41.  // draw on the generator
  42.  p.begin(&generator);
  43.  p.fillRect(0,0, 150,100, Qt::yellow);
  44.  p.drawImage(0,0, img); // this line crashes if it's a non gui application
  45.  p.end();
  46.  
  47.  
  48.  return 0;
  49. }

December 1, 2010

blex blex
Lab Rat
191 posts

What do you exactly mean when you say “gui” or “non-gui”?

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

 Signature 

————————————

Oleksiy Balabay

December 1, 2010

Henrik Henrik
Lab Rat
30 posts

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.

December 1, 2010

Henrik Henrik
Lab Rat
30 posts

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 ?

December 1, 2010

blex blex
Lab Rat
191 posts
Henrik wrote:
Can someone tell me why this is done via QPixmap ?

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

 Signature 

————————————

Oleksiy Balabay

December 2, 2010

Volker Volker
Robot Herder
5428 posts

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 http://bugreports.qt.nokia.com.

December 4, 2010

erapid erapid
Lab Rat
15 posts

  1. #0  0xb76d342d in QX11Info::appDepth(int) () from /usr/lib/libQtGui.so.4
  2. #1  0xb76d35e4 in QX11Info::depth() const () from /usr/lib/libQtGui.so.4
  3. #2  0xb7725c70 in QX11PixmapData::fromImage(QImage const&, QFlags<Qt::ImageConversionFlag>) ()
  4.    from /usr/lib/libQtGui.so.4
  5. #3  0xb770fd0c in QPixmap::fromImage(QImage const&, QFlags<Qt::ImageConversionFlag>) () from /usr/lib/libQtGui.so.4
  6. #4  0xb7776d5e in QPaintEngine::drawImage(QRectF const&, QImage const&, QRectF const&, QFlags<Qt::ImageConversionFlag>) () from /usr/lib/libQtGui.so.4
  7. #5  0xb7790e3b in QPainter::drawImage(QPointF const&, QImage const&) () from /usr/lib/libQtGui.so.4
  8. #6  0x080499e6 in QPainter::drawImage(int, int, QImage const&, int, int, int, int, QFlags<Qt::ImageConversionFlag>) ()
  9. #7  0x08049575 in main ()

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

:( 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.

December 4, 2010

blex blex
Lab Rat
191 posts

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

 Signature 

————————————

Oleksiy Balabay

December 4, 2010

Volker Volker
Robot Herder
5428 posts
blex wrote:
Is X11 should be running or just libraries should be installed?

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.

 
  ‹‹ Why doesn’t QWinHost draw, when it has a parent?      puzzle creator qt4 ››

You must log in to post a reply. Not a member yet? Register here!