ScottBell
18th November 2011, 17:39
Hi, all,
I want to _get_ the color of a standard checked button.
The purpose is to change the background of an adjacent widget to match the checked color.
I read sufficiently about palettes and style sheets, and made numerous forum searches with combinations of keywords.
After getting nowhere I repeated some tests with several brute-force combinations:
ui->myButton->setChecked(true); // ensure it is in the correct state
// no effect:
// copy myButton color
// overwrite myWidget background color
// no effect:
// copy myButton palette
// overwrite myWidget palette
// At this point I wasn't sure whether the reading or writing doesn't work.
// Style sheets are always preferred (although this is a very simple requirement)
// so I'll use it based on some examples and other answers.
QColor c;
c = ui->myButton->palette().color(QPalette::Active, QPalette::Button);
// or c = ui->myButton->palette().color(QPalette::Active, QPalette::Window);
// or c = ui->myButton->palette().color(QPalette::Active, QPalette::Base);
// or ...any other ColorRole
QString s = QString("background-color: rgb(%1,%2,%3)")
.arg(c.red()).arg(c.green()).arg(c.blue());
ui->myWidget->setStyleSheet(s);
ui->myWidget->setAutoFillBackground(true);
ui->myWidget->show();
Depending on which ColorRole, the widget background changes, so reading the palette color and setting the style sheet work.
Any ideas how to get the clicked color?
Scott
I want to _get_ the color of a standard checked button.
The purpose is to change the background of an adjacent widget to match the checked color.
I read sufficiently about palettes and style sheets, and made numerous forum searches with combinations of keywords.
After getting nowhere I repeated some tests with several brute-force combinations:
ui->myButton->setChecked(true); // ensure it is in the correct state
// no effect:
// copy myButton color
// overwrite myWidget background color
// no effect:
// copy myButton palette
// overwrite myWidget palette
// At this point I wasn't sure whether the reading or writing doesn't work.
// Style sheets are always preferred (although this is a very simple requirement)
// so I'll use it based on some examples and other answers.
QColor c;
c = ui->myButton->palette().color(QPalette::Active, QPalette::Button);
// or c = ui->myButton->palette().color(QPalette::Active, QPalette::Window);
// or c = ui->myButton->palette().color(QPalette::Active, QPalette::Base);
// or ...any other ColorRole
QString s = QString("background-color: rgb(%1,%2,%3)")
.arg(c.red()).arg(c.green()).arg(c.blue());
ui->myWidget->setStyleSheet(s);
ui->myWidget->setAutoFillBackground(true);
ui->myWidget->show();
Depending on which ColorRole, the widget background changes, so reading the palette color and setting the style sheet work.
Any ideas how to get the clicked color?
Scott