Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, July 2007
QPainter Text Drawing to QImage Segmentation Fault


Message 1 in thread

Hi,

I am try to write to a QImage from a QPainter. Here is a basic example that
generates a Segmentation Fault:

QImage image(200, 200, QImage::Format_ARGB32);
QPainter painter(&image);

painter.drawText(100, 100, "Test");

image.save("test.png");

When I use GDB to generate a backtrace, it tell me that the problem is in
the function QFontDatabase::load()

I tried everything. I have all the neccessary fonts. I'm really not sure
what's wrong. Does anyone have any ideas?

Drawing anything else, by the way, works perfectly. Also, Very Importantly,
drawing to a widget works with segmentation faults too (so when I declare
QPainter painter(this) instead of QPainter painter(&image)).

Thanks,
Alex

--
 [ signature omitted ] 

Message 2 in thread

I'm not familiar with QPainter, but I can at least add another data point
and say that I also see a segfault on Linux with Qt 4.3.0. Here is the
program:

#include <QImage>
#include <QPainter>

int main(int argc, char** argv)
{
    QImage image(200, 200, QImage::Format_ARGB32);
    QPainter painter(&image);
    painter.drawText(100, 100, "Test");
    image.save("test.png");
    return 0;
}

and here is the back trace reported by gdb:
#0  QFontCache::findEngineData (this=0x0, key=@0xbf8f112c) at
../../include/QtCore/../../src/corelib/tools/qmap.h:401
#1  0xb7b06b6b in QFontDatabase::load (d=0x97553e0, script=0) at
text/qfontdatabase.cpp:791
#2  0xb7b1bacf in QTextEngine::fontEngine (this=0xbf8f1368, si=@0x8,
ascent=0x975a278, descent=0x975a274)
    at ../../include/QtGui/private/../../../src/gui/text/qfont_p.h:147
#3  0xb7b1db23 in QTextEngine::shapeText (this=0xbf8f1368, item=0) at
text/qtextengine_unix.cpp:42
#4  0xb7b1df02 in QTextEngine::shape (this=0xbf8f1368, item=0) at
text/qtextengine.cpp:960
#5  0xb7a5c4ac in QPainter::drawText (this=0xbf8f41cc, p=@0xbf8f41b0,
str=@0xbf8f41d0) at painting/qpainter.cpp:4458
#6  0x08048856 in main ()

I should note that my Qt build is not a normal build (in particular,
-no-fontconfig is set, which may be relevant). Here is the configure
command:
./configure -release -fast -no-cups -no-fontconfig -no-opengl -no-tablet
-no-glib

I hope that helps someone else understand what is going on,
Tom

On 7/18/07, lexfridman@xxxxxxxxx <lexfridman@xxxxxxxxx> wrote:
>
> Hi,
>
> I am try to write to a QImage from a QPainter. Here is a basic example
> that
> generates a Segmentation Fault:
>
> QImage image(200, 200, QImage::Format_ARGB32);
> QPainter painter(&image);
>
> painter.drawText(100, 100, "Test");
>
> image.save("test.png");
>
> When I use GDB to generate a backtrace, it tell me that the problem is in
> the function QFontDatabase::load()
>
> I tried everything. I have all the neccessary fonts. I'm really not sure
> what's wrong. Does anyone have any ideas?
>
> Drawing anything else, by the way, works perfectly. Also, Very
> Importantly,
> drawing to a widget works with segmentation faults too (so when I declare
> QPainter painter(this) instead of QPainter painter(&image)).
>
> Thanks,
> Alex
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

Message 3 in thread

Hi,

Tom Panning wrote:
> I'm not familiar with QPainter, but I can at least add another data point
> and say that I also see a segfault on Linux with Qt 4.3.0. Here is the
> program:
> 
> #include <QImage>
> #include <QPainter>
> 
> int main(int argc, char** argv)
> {
>     QImage image(200, 200, QImage::Format_ARGB32);
>     QPainter painter(&image);
>     painter.drawText(100, 100, "Test");
>     image.save("test.png");
>     return 0;
> }

You'll have to create a QApplication instance first. Quoting from the 
QApplication doc:

"Since the QApplication object does so much initialization, it must be 
created before any other objects related to the user interface are created."

HTH, Johannes

--
 [ signature omitted ] 

Message 4 in thread

On 7/18/07, Johannes Winkelmann <jw@xxxxxxx> wrote:
>
> You'll have to create a QApplication instance first. Quoting from the
> QApplication doc:
>
> "Since the QApplication object does so much initialization, it must be
> created before any other objects related to the user interface are
> created."
>
> HTH, Johannes


Thanks, that fixed my segfault. The working code is:

#include <QApplication>
#include <QImage>
#include <QPainter>

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    QImage image(200, 200, QImage::Format_ARGB32);
    QPainter painter(&image);
    painter.drawText(100, 100, "Test");
    image.save("test.png");
    return 0;
}

Alex, does that solve your problem as well? If not, can you provide a small,
compilable example that demonstrates the problem?
Tom

Message 5 in thread

Tom Panning wrote:

> Thanks, that fixed my segfault. The working code is:
> 
> #include <QApplication>
> #include <QImage>
> #include <QPainter>
> 
> int main(int argc, char** argv)
> {
>     QApplication app(argc, argv);
>     QImage image(200, 200, QImage::Format_ARGB32);
>     QPainter painter(&image);
>     painter.drawText(100, 100, "Test");

I didn't read the thread carefully, but at least here you need:

      painter.end();

>     image.save("test.png");
>     return 0;
> }

Uwe

--
 [ signature omitted ] 

Message 6 in thread

Johannes Winkelmann wrote:

> Hi,
> 
> Tom Panning wrote:
>> I'm not familiar with QPainter, but I can at least add another data point
>> and say that I also see a segfault on Linux with Qt 4.3.0. Here is the
>> program:
>> 
>> #include <QImage>
>> #include <QPainter>
>> 
>> int main(int argc, char** argv)
>> {
>>     QImage image(200, 200, QImage::Format_ARGB32);
>>     QPainter painter(&image);
>>     painter.drawText(100, 100, "Test");
>>     image.save("test.png");
>>     return 0;
>> }
> 
> You'll have to create a QApplication instance first. Quoting from the
> QApplication doc:
> 
> "Since the QApplication object does so much initialization, it must be
> created before any other objects related to the user interface are
> created."
> 
> HTH, Johannes

Thank you Johannes and Tom.

That indeed fixes my problem in a major way. It actually also fixed my
ability to use the SvgGenerator for outputing to SVG format.

So, to recap, for those struggling with drawing using QPainter directly to
file using QImage, remember to declare an instance of QApplication before
you do anything with any class that has Q in front of it.

Thanks, you guys are great,
Alex

--
 [ signature omitted ]