[Qt-interest] text dimensions ?
Dan White
ygor at comcast.net
Wed Mar 18 00:32:50 CET 2009
OK, how about this:
void fitStringInRect ( QRectF theRect, QString theString, QPainter*
painter, Qt::Alignment flagz, qreal margin )
{
painter->save() ;
QFont foo = painter->font() ;
foo.setPixelSize ( margin * theRect.height() ) ;
QFontMetrics fm ( foo ) ;
int textWidthInPixels = fm.width( theString );
if ( textWidthInPixels > ( margin * theRect.width() ) )
{ foo.setStretch
( ( margin * theRect.width() * 100 ) / textWidthInPixels
) ;
}
painter->setFont ( foo ) ;
painter->drawText ( theRect, flagz, theString ) ;
painter->restore() ;
}
On Mar 17, 2009, at 12:33 PM, Martin Schröer wrote:
> Yes, that's where i'm coming from.
> Tested that, but that one simply ignored the text dimensions.
>
> Martin
>
> ygor at comcast.net schrieb:
>> Have you considered
>>
>> void QPainter::drawText ( const QRect(F) & rectangle, int flags,
>> const QString & text, QRect(F) * boundingRect = 0 )
>>
>> ?
>>
>> <http://doc.trolltech.com/4.5/qpainter.html#drawText-10>
>> <http://doc.trolltech.com/4.5/qpainter.html#drawText-11>
>>
>> ----- Original Message -----
>> From: Martin Schröer <gordon at gordonswelt.de>
>> To: QT Interest List <qt-interest at trolltech.com>
>> Sent: Tue, 17 Mar 2009 15:24:42 +0000 (UTC)
>> Subject: Re: [Qt-interest] text dimensions ?
>>
>> Well, okey. I thought of this approach myself but discarded it for
>> being
>> ... dirty.
>> No better way of achieving it ?
>>
>> Anyway, before i forget:
>> Thank you Jesus, for your very quick reply !
>>
>> Martin
>>
>> Jesús Fernández schrieb:
>>
>>> So, you need to try diferent dimesions and stop at the maximum size:
>>>
>>> /For example/
>>> void GraphicsShapeItem::resized()
>>> {
>>> QRectF r=boundingRect();
>>> QFont font;
>>> QFontMetrics fontMetrics;
>>> QString text=tr("Sample text");
>>> int height, width;
>>>
>>> do {
>>> font.setPointSize(font.pointSize()+1);
>>> fontMetrics=QFontMetrics(font);
>>> height=fontMetrics.height();
>>> width=fontMetrics.width(text);
>>> }while(r.width>width && r.height>height);
>>> }
>>>
>>> On Tue, Mar 17, 2009 at 4:06 PM, Martin Schröer
>>> <gordon at gordonswelt.de
>>> <mailto:gordon at gordonswelt.de>> wrote:
>>>
>>> Yes, this is nice - but only for the 'to' direction.
>>> What if i have a rectangle in which i want to render a text
>>> given ?
>>> How to find out what font size to use ?
>>>
>>>
>>> Martin
>>>
>>> Jesús Fernández schrieb:
>>>> Read this:
>>>>
>>>> http://doc.trolltech.com/4.5/qfontmetrics.html
>>>>
>>>> Regards
>>>>
>>>> On Tue, Mar 17, 2009 at 3:50 PM, Martin Schröer
>>> <gordon at gordonswelt.de <mailto:gordon at gordonswelt.de>
>>>> <mailto:gordon at gordonswelt.de <mailto:gordon at gordonswelt.de>>>
>>> wrote:
>>>>
>>>> Hi list.
>>>>
>>>> I have to drawText on a GraphicsScene.
>>>> Does anybody know a good practice to do this, considering the
>>>> [dynamic]
>>>> size of the text given to be drawn ?
>>>> I want to have the text fill a certain area in original
>>> scale, plus
>>>> having it resize along with the whole scene,
>>>> so e.g. it always fills the same region within the scene.
>>>>
>>>> Like giving a text 'rate this scene 'scary'', being always
>>>> shown
>>>> in the
>>>> bottom right corner of the scene
>>>> within a region that is scene.width() /4, scene.height() /4.
>>>>
>>>> I don't have troubles getting the size of a bounding rect
>>> for doing
>>>> that, what gives me headaches is
>>>> how to find out which size the text has or has to have,
>>> still being
>>>> drawn in aspect when the scene
>>>> dimensions changes.
>>>>
>>>> Any help, as always, highly appreciated.
>>>>
>>>> Martin
>>>> _______________________________________________
>>>> Qt-interest mailing list
>>>> Qt-interest at trolltech.com <mailto:Qt-interest at trolltech.com>
>>> <mailto:Qt-interest at trolltech.com <mailto:Qt-
>>> interest at trolltech.com>>
>>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at trolltech.com <mailto:Qt-interest at trolltech.com>
>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>
>>>
>>>
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list