Precision of QFontMetrics
-
Hi. I have the following two QLabels.


The top one has been resized using QFontMetrics, like so:
QFontMetrics fm(QFont("DejaVu Sans", 12)); //Line that stores QFont's size QString labelStyle = "color: navy; font: 12px \"DejaVu Sans\"; background: transparent; alignment: center"; //Build the main line. QFrame *frontLine = new QFrame(this); frontLine->setStyleSheet("border: 10px solid #000080;"); frontLine->setGeometry(X, Y, SPLITLINEWIDTH, SPLITLINEHEIGHT); QString labelText = "Initial Temp"; QLabel *iniTemp = new QLabel(this); iniTemp->setText(labelText); iniTemp->setStyleSheet(labelStyle); iniTemp->resize(fm.horizontalAdvance(labelText), LABELHEIGHT); //iniTemp->setFrameShadow(QFrame::Raised); iniTemp->setFrameStyle(QFrame::Panel); iniTemp->setLineWidth(2); qDebug() << (iniTemp->width()); iniTemp->move(X - (iniTemp->width())/2, Y+SPLITLINEHEIGHT+LABELDISTANCE);The bottom one has not been altered.
QFontMetrics fm(QFont("DejaVu Sans", 12)); //Line that stores QFont's size QString labelStyle = "color: navy; font: 12px \"DejaVu Sans\"; background: transparent; alignment: center"; //Build the main line. QFrame *frontLine = new QFrame(this); frontLine->setStyleSheet("border: 10px solid #000080;"); frontLine->setGeometry(X, Y, SPLITLINEWIDTH, SPLITLINEHEIGHT); QString labelText = "Initial Temp"; QLabel *iniTemp = new QLabel(this); iniTemp->setText(labelText); iniTemp->setStyleSheet(labelStyle); //iniTemp->resize(fm.horizontalAdvance(labelText), LABELHEIGHT); //iniTemp->setFrameShadow(QFrame::Raised); iniTemp->setFrameStyle(QFrame::Panel); iniTemp->setLineWidth(2); qDebug() << (iniTemp->width()); iniTemp->move(X - (iniTemp->width())/2, Y+SPLITLINEHEIGHT+LABELDISTANCE);How precise is QFontMetrics ususally? I wanna know this because I believe my fonts are correctly labelled unless I've misunderstood something. Please let me know if more information is required.
-
@mrjj Thank you for your response. I am currently operating on Windows 10, is that enough additional information?
Hi
Well in that case it should be pretty much on spot. At least in win 7/10.Do note that sometimes mixing Stylesheets and Fonts might give surprises. :)
-
Hi
Normally QFontMetrics is pretty precise, and horizontalAdvance even more so than the old Width function but
it also depends on the OS. -
Hi
Normally QFontMetrics is pretty precise, and horizontalAdvance even more so than the old Width function but
it also depends on the OS.@mrjj Thank you for your response. I am currently operating on Windows 10, is that enough additional information?
-
@mrjj Thank you for your response. I am currently operating on Windows 10, is that enough additional information?
Hi
Well in that case it should be pretty much on spot. At least in win 7/10.Do note that sometimes mixing Stylesheets and Fonts might give surprises. :)
-
Hi
Well in that case it should be pretty much on spot. At least in win 7/10.Do note that sometimes mixing Stylesheets and Fonts might give surprises. :)
@mrjj I see, thanks. In that case, what are some other ways I can control the font size of the text in QLabel? I haven't been able to find any within the QLabel documentation.
-
@mrjj I see, thanks. In that case, what are some other ways I can control the font size of the text in QLabel? I haven't been able to find any within the QLabel documentation.
@Dummie1138 said in Precision of QFontMetrics:
what are some other ways I can control the font size of the text in QLabel?
auto label=new QLabel("Hello Wolrd"); auto font=label->font(); // default font font.setPointSize(font.pointSize()+2); label->setFont(font); label->show();