PDA

View Full Version : Changing the color of a checkbox in a dialog window, when it is checked/unchecked



prykHetQuo
30th January 2009, 09:43
I have a dialog window with many checkboxes. I want to change the colour of a checkbox, when it is changed (checked or unchecked).

AFAIK, the easiest way to do this, is with event handlers. However can I implement an event handler in the dialog window class, that knows which checkbox was changed, so as to process it, and how?


Thanks a lot.

prykHetQuo
30th January 2009, 11:46
Actually I implemented an "event filter":



for(unsigned j= 1; j<= 8; ++j)
{
// Adds the empty checkboxes

QCheckBox *checkBox= new QCheckBox(scrollAreaWidgetContents);

checkBox->setObjectName(QString("checkBox_%1%2").arg(QString::number(i)).arg(QString::number(j))) ;
checkBox->setGeometry(QRect(50* (j+ 1)+ 37, 20* i, 83, 20));

// We change the color of the check boxes to blue, using RGB.
checkBox->setPalette (QPalette(QColor(0, 0, 255)));

//We make the Dialog object an event filter for all events of every check box.
checkBox->installEventFilter(this);
}


// Event handler for the check boxes' events
bool Dialog::eventFilter(QObject *target, QEvent *event)
{
using namespace std;

if(event->type()== QEvent::MouseButtonRelease)
{
QCheckBox *checkBox= static_cast<QCheckBox *>;(target);

checkBox->setPalette(QPalette(QColor(255, 0, 0)));

return false;
}

else
return QDialog::eventFilter(target, event);
}


However there is the side-effect, of a moving blue rectanglualr, as shown in the following screenshot:

http://www.cpp-software.net/temp/s.png

Any ideas on how to solve that?

spirit
30th January 2009, 12:05
actually you don't need to install event filter for this issue, all what you need it's to handle


void QAbstractButton::toggled ( bool checked )

and change QPalette.

Lesiok
30th January 2009, 13:06
I think that You must read about stylesheets (http://doc.trolltech.com/4.4/stylesheet-reference.html)

prykHetQuo
31st January 2009, 14:52
I think that You must read about stylesheets (http://doc.trolltech.com/4.4/stylesheet-reference.html)

Where do we write the stylesheets?

For example the

QCheckBox { color: blue }


I have tried inside the code, but I get an error.

faldzip
31st January 2009, 15:37
I think that best way to learn about style sheets is to type "style sheet" in Qt Assistant and read some docs. (or just look here: stylesheets)

But, anyway, you have to do like this:


qApp->setStyleSheet("QCheckBox { color: blue }");

and now all check boxes in your app will be blue.

prykHetQuo
1st February 2009, 12:19
I think that best way to learn about style sheets is to type "style sheet" in Qt Assistant and read some docs. (or just look here: stylesheets)

But, anyway, you have to do like this:


qApp->setStyleSheet("QCheckBox { color: blue }");

and now all check boxes in your app will be blue.

I had already tried:

app.setStyleSheet("QCheckBox { color: blue }");

checkBox->setStyleSheet("QCheckBox { color: blue }");

and

checkBox->setStyleSheet("* { color: blue }");


and they do not work. However the "background: blue" works.


However this works for the foreground color:

checkBox->setPalette(QColor(0, 0, 255));


An extract of my code:



// We change the color of all QCheckBox objects to blue using a Style Sheet.
// In my platform this has no apparent effect, but it eliminates the blue/red rectangulars
// appearing after the calls to QWidget::setPalette() of checkBox.
// checkBox->setStyleSheet("QCheckBox { background: blue }"); works OK.
checkBox->setStyleSheet("QCheckBox { color: blue }");

// We change the color of the check boxes to blue (RGB).
checkBox->setPalette(QColor(0, 0, 255));

faldzip
1st February 2009, 17:28
OK, I didn't check what "color" means to check boxes. But you can check in style sheet reference. There are guidelines for every widget I think, and lists of attributes you can change, so if "color" is not working maybe it's something else.

as I checked in docs:
color | Brush | The color used to render text.

so if you have no text, no chage is as a result of your code (as I understand it right).

P.S. If you don't know, there is some tool included to the Qt and it's called Qt Assistant. You can check there anything you want about any Qt class and feature, so maybe try looking there sometimes - it can speed up you projects progress as it takes less time then waiting for reply on forum. Ofcourse, if you still have some problems/questions ask them here and we will help.

prykHetQuo
2nd February 2009, 13:25
I am using Qt Assistant.