Results 1 to 3 of 3

Thread: Need some help re-painting a checkbox

  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Need some help re-painting a checkbox

    Hi, I'd like to replace the default checkbox with images of my own, but I'm having some difficulties. Just doesn't work, as if nothing is happening. Here is the code where I try to "catch" the checkbox and redraw, keep in mind I don't really know what I'm doing, just feeling my way:

    Qt Code:
    1. void ResoStyle::drawControl(ControlElement control, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
    2. {
    3. switch (control)
    4. {
    5. //replaces default CheckBox style with our own
    6. case CE_CheckBox:
    7. if(const QStyleOptionButton * checkOption = qstyleoption_cast<const QStyleOptionButton *>(option))
    8. {
    9. const QCheckBox * checkbox = qobject_cast<const QCheckBox *>(widget);
    10.  
    11. QStyleOptionButton newCheckBox(*checkOption);
    12.  
    13. QRect frame = newCheckBox.rect;
    14.  
    15.  
    16. painter->save();
    17. painter->setBrushOrigin(frame.topLeft());
    18.  
    19. if(newCheckBox.state & State_MouseOver)
    20. {
    21. if(!checkbox->isChecked())
    22. {
    23. painter->drawImage(frame, QImage("./Images/disabled_hover.png"));
    24. }
    25. else
    26. {
    27. //draw the color of the current module class
    28. painter->drawImage(frame, QImage("./Images/personal_enabled.png"));
    29. }
    30. }
    31. else
    32. {
    33. if(!checkbox->isChecked())
    34. {
    35. painter->drawImage(frame, QImage("./Images/disabled_default.png"));
    36. }
    37. else
    38. {
    39. //draw the color of the current module class
    40. painter->drawImage(frame, QImage("./Images/personal_enabled.png"));
    41. }
    42. }
    43. painter->restore();
    44. QCleanlooksStyle::drawControl(control, &newCheckBox, painter, widget);
    45. }
    46. else
    47. {
    48. QCleanlooksStyle::drawControl(control, option, painter, widget);
    49. }
    50. break;
    To copy to clipboard, switch view to plain text mode 

    One thing I'd also like to understand is, is this the best way to do something like this. Would I also be able to just override the paintEvent for the widget and redraw there just as well?

    Thanks for any help you can give...

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need some help re-painting a checkbox

    Are style sheets out of question..? I believe it would be pretty much straightforward to do with "pseudo states".
    J-P Nurmi

  3. #3
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Need some help re-painting a checkbox

    Quote Originally Posted by jpn View Post
    Are style sheets out of question..? I believe it would be pretty much straightforward to do with "pseudo states".
    Yeah, I've thought about using style sheets, I may play with them and see what I can get. I was hesitant about shoe-horning them into my code though, as the rest of my code uses this QStyle. Thanks for the tip!

Similar Threads

  1. Replies: 3
    Last Post: 1st April 2011, 04:58
  2. Slow painting in QGraphicsView
    By JonathanForQT4 in forum Qt Programming
    Replies: 12
    Last Post: 16th July 2007, 09:54
  3. Painting in QCanvasView
    By JimBrown in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2007, 22:29
  4. QSpinBox with checkbox
    By :db:sStrong in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2007, 13:22
  5. checkbox
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2006, 21:59

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.