How to customize text on Qpushbutton using QPalette?

7.1k Views Asked by At

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

2

There are 2 best solutions below

0
On

How to set font, size and color for the Text of QPushButton in Qt5.6.1 using QPalette?

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 use QWidget::setFont() to change font family/size. For example:

QPushButton button("Press Me");
//get initial default palette
QPalette pal= button.palette();
//change some color roles
pal.setColor(QPalette::Window, QColor(255, 0, 0)); //red color
pal.setColor(QPalette::Button, QColor(0x00, 0x00, 0xff)); //blue color (hex representation)
pal.setColor(QPalette::ButtonText, QColor(Qt::yellow)); //Qt global color
button.setPalette(pal);
//change font family and size
button.setFont(QFont("Helvetica [Cronyx]", 15, QFont::Bold));

In order for palettes to work properly, you may want to first use Fusion style in the application by calling:

QApplication::setStyle(QStyleFactory::create("Fusion"));

before instantiating the QApplication instance in main() 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:

screenshot

#include <QtWidgets>

int main(int argc, char** argv)
{
    QApplication::setStyle(QStyleFactory::create("Fusion"));
    QPalette pal= QApplication::palette();
    pal.setColor(QPalette::Window, QColor(Qt::red));
    pal.setColor(QPalette::Button, QColor(Qt::blue));
    pal.setColor(QPalette::ButtonText, QColor(Qt::yellow));
    pal.setBrush(QPalette::WindowText, QBrush(QColor(Qt::yellow)));
    pal.setColor(QPalette::Highlight, QColor(Qt::green));
    //further palette customization. . . 
    QApplication::setPalette(pal);
    QApplication::setFont(QFont("Helvetica [Cronyx]", 15, QFont::Bold));

    QApplication a(argc, argv);

    //setup and show GUI components

    return a.exec();
}
0
On

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"));