PDA

View Full Version : How to get the color of "checked" QPushButton



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

grin
19th November 2011, 14:31
Hi, I made some test - result - pallete colors doesn't changed for checked or unchecked button:


#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui>

class CWidget : public QWidget
{
Q_OBJECT
QPushButton*m_but;
QMap<int,QMap<int,QColor> >m_initial_colors;
QMap<int,QMap<int,QColor> >m_checked_colors;
QMap<int,QMap<int,QColor> >m_unchecked_colors;
public:
CWidget(QWidget*parent=0) : QWidget(parent)
{
m_but = new QPushButton("Button",this);
m_but->setCheckable(true);
setFixedSize(300,300);
m_but->setGeometry(100,100,100,100);
butToggled(false);
connect(m_but,SIGNAL(toggled(bool)),SLOT(butToggle d(bool)));
}
private slots:
void storeRoleColor(QMap<int,QMap<int,QColor> >&map,QPalette::ColorGroup group,QPalette::ColorRole role)
{
map[group][role] = m_but->palette().color(group,role);
}

void storeGroupColors(QMap<int,QMap<int,QColor> >&map,QPalette::ColorGroup group)
{
storeRoleColor(map,group,QPalette::Window);
storeRoleColor(map,group,QPalette::Background);
storeRoleColor(map,group,QPalette::WindowText);
storeRoleColor(map,group,QPalette::Foreground);
storeRoleColor(map,group,QPalette::Base);
storeRoleColor(map,group,QPalette::AlternateBase);
storeRoleColor(map,group,QPalette::ToolTipBase);
storeRoleColor(map,group,QPalette::ToolTipText);
storeRoleColor(map,group,QPalette::Text);
storeRoleColor(map,group,QPalette::Button);
storeRoleColor(map,group,QPalette::ButtonText);
storeRoleColor(map,group,QPalette::BrightText);
}

void storeAllColors(QMap<int,QMap<int,QColor> >&map)
{
storeGroupColors(map,QPalette::Disabled);
storeGroupColors(map,QPalette::Active);
storeGroupColors(map,QPalette::Inactive);
}

void butToggled(bool checked)
{
if(m_initial_colors.empty()) {
storeAllColors(m_initial_colors);
} else {
bool print = false;
if(checked) {
storeAllColors(m_checked_colors);
print = ! m_unchecked_colors.empty();
} else {
storeAllColors(m_unchecked_colors);
print = ! m_checked_colors.empty();
}
if(print) {
qDebug() << "result: ";
QMap<int,QMap<int,QColor> >::const_iterator group = m_initial_colors.constBegin();
for(;group != m_initial_colors.constEnd();group++) {
QMap<int,QColor>::const_iterator role = group.value().constBegin();
for(;role != group.value().constEnd();role++) {
int g_id = group.key();
int r_id = role.key();
QColor initial = m_initial_colors[g_id][r_id];
QColor checked = m_checked_colors[g_id][r_id];
QColor unchecked = m_unchecked_colors[g_id][r_id];
QString res;
if( (initial == checked) && (checked == unchecked)) {
res = "SAME";
} else if(checked == unchecked) {
res = "PARTITIALY SAME";
} else {
res = "DIFFERENT";
}

qDebug() << "[" << g_id << "," << r_id << "] "
<< initial << " : " << checked << " : " << unchecked << " - " << res;

}
}
}
}
}
};

#endif // WIDGET_H


I think that style (with finaly paint standard button) is response that checked button's background is other than unchecked button.

You can set background color for you button directly (through styleSheet, or through setting pallete after toggling) - this way allows you control button's background at any state.

P.S. in test above all case response "SAME"

ScottBell
21st November 2011, 21:15
Thanks, grin, for now I've done a workaround.

The primary reason for wanting to get the color is to handle other platforms -- no matter where the app is running, the window next to the clicked button looks the same as the button. And also so it doesn't make the user ralph because the override I use doesn't agree with his/her color scheme.

This doesn't seem like a way-out request, among the hundreds of functions Qt provides for customization.