PDA

View Full Version : Position of text in a qlabel



jiveaxe
16th May 2008, 16:38
Hi,
I have created a custom label where text is compressed if wider than object; here the code:


void CustomLabel::paintEvent(QPaintEvent * /* event */)
{
QPainter *painter = new QPainter(this);
painter->setRenderHints(QPainter::HighQualityAntialiasing,t rue);
QAbstractTextDocumentLayout::PaintContext context;
QTextDocument doc;
doc.setDefaultFont(font());
doc.setHtml(text());
float ratio = doc.documentLayout()->documentSize().width() / width();
if(ratio > 1.0)
painter->scale(1.0/ratio,1.0);
doc.documentLayout()->draw(painter, context);
delete painter;
}

It works; but if I have 2 sided labels, one normal qlabel and one customlabel the text in the custom label has a base line which is lower; here a picture of what I mean: the three labels on the left are qlabels and those with bold text are customlabels

http://img111.imageshack.us/img111/7808/imagecd1.th.png (http://img111.imageshack.us/my.php?image=imagecd1.png)

I have used Designer to create the gui and, from designer itself, I have promoted the labels to customlabel.

Has QLabel a property which returns text position or something else to adjust the text position in my label?

Thanks.

jpn
25th May 2008, 13:26
Why don't you take a look at QLabel::paintEvent() implementation and see yourself? :)

PS. Attach images instead of using an external site next time, please.