How to set font, size and color for the Text of QPushButton in Qt5.6.1 using QPalette? As I am curious to know how we can set these properties without using QStyleSheet in Qt. Also how we can set hex code of color in QT. Please suggest any way to do with QPalette. Thanks
How to customize text on Qpushbutton using QPalette?
7.1k Views Asked by Avi At
2
There are 2 best solutions below
0

I agree with the above mentioned points.
Following is the another approach to achieve the Font Customization with QWidgets.
QPushButton *button3 = new QPushButton("CheckFont", this);
button3->setGeometry(QRect(QPoint(50,50),QSize(200,50)));
QFont FontObj;
FontObj.setFamily(QStringLiteral("Helvetica [Cronyx]"));
FontObj.setPointSize(15);
FontObj.setBold(true);
FontObj.setUnderline(true);
button3->setFont(FontObj);
Also when Color code is in format like "#ffffff"
(which is white color code) then you can use following way to set the hex color code with QPalette.
pal.setColor(QPalette::Button, QColor(QRgb("#ffffff")));
OR
pal.setColor(QPalette::Button, QColor("#ffffff"));
A
QPalette
contains color groups for each widget state. It does not contain font/size information.You can use
QWidget::setPalette()
to change specific color roles (Button, ButtonText, Window. . .), and useQWidget::setFont()
to change font family/size. For example:In order for palettes to work properly, you may want to first use Fusion style in the application by calling:
before instantiating the
QApplication
instance inmain()
because some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows XP, Windows Vista, and the OS X styles.Also, You may want to set the palette/font on the whole application, so that you get those colors in all widgets in the application. Something like this: