[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    question about QPainter::boundingRect()
From:       Florian Merz <FlorianMerz () gmx ! de>
Date:       2008-03-21 16:30:06
Message-ID: 200803211730.07407.FlorianMerz () gmx ! de
[Download RAW message or body]

Hi everyone,

I was hoping, that someone on this list can help me with a problem that I 
have with QPainter::boundingRect().

I'm using QPainter to draw a short text at a specified position. I want to 
paint a colored rectangle below the text to make sure that the text is 
always readable.
Up to now I've been doing it like this:

    QRectF label(position, QSizeF(70.0, 12.0));

    painter.fillRect(label, QColor(Qt::red));
    painter.setPen(QPen(Qt::white));
    painter.drawText(label, Qt::AlignHCenter | Qt::AlignVCenter, text);

Now I'd like to change this to use QPainter::boundingRect() so that the 
rectangle around the text always has the right size for the text. I changed 
the code to this:

    QRectF label = painter.boundingRect(QRectF(position, QSizeF(70.0,12.0)),
            Qt::AlignHCenter | Qt::AlignVCenter, text);

    painter.fillRect(label, QColor(Qt::red));
    painter.setPen(QPen(Qt::white));
    painter.drawText(label, Qt::AlignHCenter | Qt::AlignVCenter, text);

For some reason boundingRect() always returns an empty rectangle, so I must 
be doing something wrong with the boundingRect() call. But position, size, 
flags and text are exactly like the call the drawText() itself, and that 
paints the text exactly as it should.

Thanks for any help,
 Florian
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic