QDialogButtonBox: Is there any way to fill different colors to Individual Buttons?
-
13-07-2021 - |
سؤال
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 !!!!
المحلول
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.
نصائح أخرى
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;
}
لا تنتمي إلى StackOverflow