Results 1 to 9 of 9

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

  1. #1
    Join Date
    Jan 2009
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11

    Arrow Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    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.

  2. #2
    Join Date
    Jan 2009
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11

    Arrow Re: Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    Actually I implemented an "event filter":

    Qt Code:
    1. for(unsigned j= 1; j<= 8; ++j)
    2. {
    3. // Adds the empty checkboxes
    4.  
    5. QCheckBox *checkBox= new QCheckBox(scrollAreaWidgetContents);
    6.  
    7. checkBox->setObjectName(QString("checkBox_%1%2").arg(QString::number(i)).arg(QString::number(j)));
    8. checkBox->setGeometry(QRect(50* (j+ 1)+ 37, 20* i, 83, 20));
    9.  
    10. // We change the color of the check boxes to blue, using RGB.
    11. checkBox->setPalette (QPalette(QColor(0, 0, 255)));
    12.  
    13. //We make the Dialog object an event filter for all events of every check box.
    14. checkBox->installEventFilter(this);
    15. }
    16.  
    17.  
    18. // Event handler for the check boxes' events
    19. bool Dialog::eventFilter(QObject *target, QEvent *event)
    20. {
    21. using namespace std;
    22.  
    23. if(event->type()== QEvent::MouseButtonRelease)
    24. {
    25. QCheckBox *checkBox= static_cast<QCheckBox *>;(target);
    26.  
    27. checkBox->setPalette(QPalette(QColor(255, 0, 0)));
    28.  
    29. return false;
    30. }
    31.  
    32. else
    33. return QDialog::eventFilter(target, event);
    34. }
    To copy to clipboard, switch view to plain text mode 

    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?

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    I think that You must read about stylesheets

  5. #5
    Join Date
    Jan 2009
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    Quote Originally Posted by Lesiok View Post
    I think that You must read about stylesheets
    Where do we write the stylesheets?

    For example the

    QCheckBox { color: blue }


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

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    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:
    Qt Code:
    1. qApp->setStyleSheet("QCheckBox { color: blue }");
    To copy to clipboard, switch view to plain text mode 
    and now all check boxes in your app will be blue.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    Jan 2009
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    Quote Originally Posted by faldżip View Post
    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:
    Qt Code:
    1. qApp->setStyleSheet("QCheckBox { color: blue }");
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. // We change the color of all QCheckBox objects to blue using a Style Sheet.
    2. // In my platform this has no apparent effect, but it eliminates the blue/red rectangulars
    3. // appearing after the calls to QWidget::setPalette() of checkBox.
    4. // checkBox->setStyleSheet("QCheckBox { background: blue }"); works OK.
    5. checkBox->setStyleSheet("QCheckBox { color: blue }");
    6.  
    7. // We change the color of the check boxes to blue (RGB).
    8. checkBox->setPalette(QColor(0, 0, 255));
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    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.
    Last edited by faldzip; 1st February 2009 at 16:40.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  9. #9
    Join Date
    Jan 2009
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Changing the color of a checkbox in a dialog window, when it is checked/unchecked

    I am using Qt Assistant.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.