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

List:       kde-devel
Subject:    Implementing a Label which resizes fonts on resize events
From:       Giacomo <delleceste () gmail ! com>
Date:       2009-11-16 7:49:06
Message-ID: 885896af0911152349s511e56b8nffdbd0b3ff735d88 () mail ! gmail ! com
[Download RAW message or body]

Hi to all.
I am implementing some widgets that display strings from
QLineEdit, QLabel,
QPushButton and when they receive resize events increase
or decrease their font size to
fit the available space.
The font size is calculated trough fontMetrics, given the
string that the widget
represents.
For QPushButton and QLineEdit I succeeded in finding out
the correct rectangle
where the text is drawn by means of QStyleOptionButton and
QStyleOptionFrame, for the
QLineEdit.
When the widget is resized, from the style() i calculate
the rectangle according to
the QStyleOption and I increase the font until it fits the
rectangle size.

The problem is QLabel: it inherits from QFrame and I do
not understand how to calculate
the area where the text is drawn.
The issue arises inside widgets with a Layout: font
changes trigger resize events in widgets
inserted in a layout causing them to continuously grow and
occupy all the space available in
the layout. The correct calculation of the available space
for the text in QPushButton and
QLineEdit avoided the problem, but for QLabel I do not
know how to do.

What I did, not enough, is take the size() of the QLabel
and subtract the frame width and
mid line width:

QSize ESimpleLabel::calculateTextSpace()
{
     QSize size;
    /* this is unuseful: returns a size of 0 unless a
stylesheet is provided:
     * QStyleOption labelStyleOption;
     * labelStyleOption.initFrom(this);
     * size =
style()->subElementRect(QStyle::SE_FrameContents,
&labelStyleOption, this).size();
     */
    /* try another way */
    size = size(); /* full size of the widget */
    size.setWidth(size.width() - 4 * frameWidth() - 2 *
midLineWidth());
    size.setHeight(size.height() - 4 * frameWidth() - 2 *
midLineWidth());
    /* although frameWidth() is multiplied by 4, when the
QLabel is resized big, it explodes
     * in the layout.
     */
    return size;

}

A note: if the aligment of the text is Qt::AlignHCenter,
it scales ok, otherwise it starts
looping through resize events.

Thanks for any hint to correctly calculate the size
needed.

Giacomo,

Elettra - Synchrotron Radiation Facility
Trieste, Italy.


-- 
Giacomo S.
http://www.giacomos.it

- - - - - - - - - - - - - - - - - - - - - -

* Aprile 2008: iqfire-wall, un progetto
  open source che implementa un
  filtro di pacchetti di rete per Linux,
  e` disponibile per il download qui:
  http://sourceforge.net/projects/ipfire-wall

* Informazioni e pagina web ufficiale:
  http://www.giacomos.it/iqfire/index.html

- - - - - - - - - - - - - - - - - - - - - -

 . ''  `.
:   :'    :
 `.  ` '
    `- Debian GNU/Linux -- The power of freedom
        http://www.debian.org
 
>> 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