Hi,
I have created a custom label where text is compressed if wider than object; here the code:
void CustomLabel
::paintEvent(QPaintEvent * /* event */) {
painter
->setRenderHints
(QPainter::HighQualityAntialiasing,
true);
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;
}
void CustomLabel::paintEvent(QPaintEvent * /* event */)
{
QPainter *painter = new QPainter(this);
painter->setRenderHints(QPainter::HighQualityAntialiasing,true);
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;
}
To copy to clipboard, switch view to plain text mode
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
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.
Bookmarks