Question

I am creating an Error Message Box for my Screen. I am using QDialogButtonBox for buttons. Now i want to fill Buttons with different colors. For example: "OK" --> Green "Cancel" --> Red etc. I am able to change the background for all buttons , but not individually.

Is there any way to do this ??

Thanks in advance !!!!

Was it helpful?

Solution

Try this(using QDialogButtonBox::button() and QPushButton::setStyleSheet()).

QDialogButtonBox* buttonBox = new QDialogButtonBox;
// set up your button box
QColor okButtonColor = Qt::red;
buttonBox->button(QDialogButtonBox::Ok)->setStyleSheet(QString("background:%1").arg(okButtonColor.name()));

EDITED: typo fixed in code around building style sheet string.

OTHER TIPS

I know I am late, but I am gonna leave this for a felow developer who went through the same problem as I did, this works for both python and c++ developers, you can use this to change color of a specific button:

#<QDialogButtonBox ObjectName> *[text="Save"] {
    background: red;
}
#<QDialogButtonBox ObjectName> *[text="OK"] {
    background: green;
}

Example on Qt Designer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top