Solved Setting font to QLabel failled
-
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?