Solved Setting font to QLabel failled
-
Hi,
I have a widget with a QLabel and a QLineEdit and I want to be able to modify the font of the QLineEdit without affecting the QLabel font. So I reimplemented the setFont of the widget. I've first try to only apply the new font to the line edit, but if I don't call the base class implementation (QWidget::setFont()) it bugs and nothing is displayed (neither QLabel, neither QLineEdit).
So I've change to call the base class implementation in my implementation, which makes both QLineEdit and QLabel font change.
So I saved the QLabel original font before and reapply it after... It does'nt work neither. But if I specify a static font it works.
Code exemple:class MyWidget: public QWidget { virtual void setFont(const QFont& font); private: QLineEdit* lineedit; QLabel* label; }
first case:
void MyWidget::setFont(const QFont& newFont) { lineedit->setFont(newFont); }//nothing is displayed
second case:
void MyWidget::setFont(const QFont& newFont) { QFont f = label.font(); //storing label font QWidget::setFont(font); //applying new font to the whole widget label->setFont(f); //applying back its original font to label }//label font is set to newFont and not to f
third case:
void MyWidget::setFont(const QFont& newFont) { QFont f("Times", 8); //creating a local variable font QWidget::setFont(font); //applying new font to the whole widget label->setFont(f); //applying the local font to label }//label font is set to f
this is working in the third case, but I would like to keep the original font of label and not code it in a static way in case the general style of the application change.
Any idea? -
You overcomplicated the issue a lot. just call
lineedit->setStyleSheet("font: bold italic large "Times New Roman");
-
@VRonin said in Setting font to QLabel failled:
You overcomplicated the issue a lot. just call
lineedit->setStyleSheet("font: bold italic large "Times New Roman");
I personally am not a fan of setting fonts via stylesheet.
For example, what if you want an option to change the font size in your app via a QSpinbox, so the elderly can read your texts too :-)
You have to create a StyleSheet as a QString, insert the new fontsize into it, and apply it to all effected Objects/Widgets.
Or am I wrong here?
-
Hi and welcome to devnet,
Just a side note, the
setFont
method is not virtual so your code is hiding the base class implementation thus you won't have the results you expect. -
@H_V_ Just In case, my customize function name first character is capitalized. like
Class::SetFont(...)
. qt inner functionClass::setFont(...)
; -
@VRonin How can i set a stylesheet that depend on a font that is choose by the user? Because your solution seems a bit static to me...
-
@SGaist I agree, that's why I call the QWidget::setFont
-
How can i set a stylesheet that depend on a font that is choose by the user?
#include <QStringBuilder> QString fontStyleString(const QFont& fnt){ const QString baseStr= QStringLiteral("font-family: \"") % fnt.family() % QStringLiteral("\";font-weight: ") % (fnt.bold() ? QStringLiteral("bold") : QStringLiteral("normal")) % QStringLiteral(";font-size: ") % QString::number(fnt.pointSizeF(),'f') % "pt" % QStringLiteral(";font-style: "); switch (fnt.style()){ case QFont::StyleItalic: return baseStr % QStringLiteral("italic"); case QFont::StyleOblique: return baseStr % QStringLiteral("oblique"); default: return baseStr % QStringLiteral("normal"); } }
-
@VRonin Thanks, it works perfectly.
But I'm a bit curious on why my method works with the local variable and not with the stored value. If someone has an explaination I would be grateful... -
@H_V_ said in Setting font to QLabel failled:
But I'm a bit curious on why my method works with the local variable and not with the stored value. If someone has an explaination I would be grateful...
strange indeed. funnily enough this works:
const QString f = label->font().toString(); //storing label font QWidget::setFont(font); //applying new font to the whole widget QFont tmpFont; tmpFont.fromString(f); label->setFont(tmpFont);
-
Ok, I'm so sorry guys, I think I was clearly not awake enough.
After more investigation, it appears that the first described case works perfectly when you don't forget to clean your project :-s :void MyWidget::setFont(const QFont& newFont)
{
lineedit->setFont(newFont);
}So sorry for wasting your time and thanks a lot for all your replies
-
@VRonin said in Setting font to QLabel failled:
@H_V_ said in Setting font to QLabel failled:
But I'm a bit curious on why my method works with the local variable and not with the stored value. If someone has an explaination I would be grateful...
strange indeed. funnily enough this works:
const QString f = label->font().toString(); //storing label font QWidget::setFont(font); //applying new font to the whole widget QFont tmpFont; tmpFont.fromString(f); label->setFont(tmpFont);
Despite I found the solution, I'm still curious about that strange behavior (that appears even stranger after your post)... Have you found any explaination?