Results 1 to 17 of 17

Thread: QStyle button hover

  1. #1
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QStyle button hover

    How can i make QPushButton look like hovered by mouse using QStyle? Maybe Style Flags or something else?

  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: QStyle button hover

    I recommend taking a look at sources when you want to find these things out (QPushButton::initStyleOption() or something similar in your case). It's a state flag passed in QStyleOption.
    J-P Nurmi

  3. #3
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle button hover

    I know that i can do it by using QStyle::State :
    Qt Code:
    1. void form1:;paintEvent(QPaintEvent*) {
    2. sob.initFrom(this);
    3. QPainter painter(this);
    4. sob.rect=QRect(10,10,50,50);
    5. sob.state=QStyle::State(QStyle::State_MouseOver);
    6. style()->drawControl(QStyle::CE_PushButton, &sob, &painter, this);
    7. }
    To copy to clipboard, switch view to plain text mode 

    But this given't result, button still look like State_Enabled. Maybe i did something wrong?

  4. #4
    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: QStyle button hover

    They are flags. You should combine a set of flags by using OR operator:
    Qt Code:
    1. sob.state |= QStyle::State_MouseOver;
    To copy to clipboard, switch view to plain text mode 
    Notice what QStyleOption::initFrom() docs say.. It already initializes state so basically you overrode it with plain QStyle::State_MouseOver.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    Apocalypse (30th March 2008)

  6. #5
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle button hover

    One more question. What flag can make that button look like Disabled?

  7. #6
    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: QStyle button hover

    So remove QStyle::State_Enabled which has been turned on by QStyleOption::initFrom(). It's all about simple bitwise operations
    Qt Code:
    1. sob.state &= ~QStyle::State_Enabled;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. #7
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle button hover

    It did nothing. Maybe i must delete some more flags?

  9. #8
    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: QStyle button hover

    Quote Originally Posted by Apocalypse View Post
    It did nothing. Maybe i must delete some more flags?
    Are you sure? Which style is that? With some styles it's just hard to notice the difference when there's no text:
    Qt Code:
    1. sob.text = "Button";
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. #9
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle button hover

    No, i'm using Oxygen style and ~QStyle::State_Enabled doesn't works for me. Maybe i must do something else?

  11. #10
    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: QStyle button hover

    Try with other style to verify it works? Well, at least it works perfectly for me.
    J-P Nurmi

  12. #11
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle button hover

    It doesn't works with other styles. Do you know another variants?

  13. #12
    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: QStyle button hover

    Could you show the current code of yous and explain what does "does not work" mean in your case?
    J-P Nurmi

  14. #13
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle button hover

    Sure. It means that ~QStyle::State_Enabled given't result and button still look like enabled. To make button disabled i use :
    Qt Code:
    1. QPainter painter(this);
    2. sob.initFrom(this);
    3. sob.rect=QRect(5,55, 93,22);
    4. sob.state = QStyle::State_Sunken;
    5. style()->drawControl(QStyle::CE_PushButton, &sob, &painter, this);
    6. sob.rect=QRect(5,80, 93,22);
    7. sob.state &= ~QStyle::State_Enabled & ~QStyle::State_Sunken;
    8. style()->drawControl(QStyle::CE_PushButton, &sob, &painter, this);
    To copy to clipboard, switch view to plain text mode 

    ~QStyle::State_Sunken works and button become enabled. But i want to make it disabled. I tried to delete initFrom, but it didn't worked too;

  15. #14
    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: QStyle button hover

    Could you do me a favor: compile the following code and run it with "-style motif" arguments.

    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. class Widget : public QWidget
    5. {
    6. public:
    7. void paintEvent(QPaintEvent*)
    8. {
    9. QPainter painter(this);
    10.  
    11. opt.state = 0;
    12. opt.text = "Disabled";
    13. opt.rect = QRect(0,0,100,25);
    14. style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
    15.  
    16. opt.state = QStyle::State_Enabled;
    17. opt.text = "Enabled";
    18. opt.rect = QRect(100,0,100,25);
    19. style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
    20.  
    21. opt.state = QStyle::State_Sunken;
    22. opt.text = "Disabled,Sunken";
    23. opt.rect = QRect(0,25,100,25);
    24. style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
    25.  
    26. opt.state = QStyle::State_Enabled|QStyle::State_Sunken;
    27. opt.text = "Enabled,Sunken";
    28. opt.rect = QRect(100,25,100,25);
    29. style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
    30. }
    31. };
    32.  
    33. int main(int argc, char* argv[])
    34. {
    35. QApplication a(argc, argv);
    36. Widget w;
    37. w.show();
    38. return a.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  16. #15
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle button hover

    Yes, you are right. The button looks like disabled in motif and other squared styles.
    But i need another effect. To explain what i want i made next addition into your code:
    Qt Code:
    1. Widget()
    2. {
    3. QPushButton *button = new QPushButton(this);
    4. button->move(0,50);
    5. button->setDisabled(true);
    6. button->setText("Disabled PushButton");
    7. button->show();
    8. this->resize(200,200);
    9. }
    To copy to clipboard, switch view to plain text mode 

    And i made 4 screensots off app in few styles. You can find it in attachment. Could you tell me how can i draw my button same as "Disabled PushButton"?
    Attached Images Attached Images

  17. #16
    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: QStyle button hover

    Try setting the palette to use "disabled" colors:
    Qt Code:
    1. // sets the palette of this
    2. opt.initFrom(this);
    3. // "this" is not disabled so we switch the color group by hand
    4. opt.palette.setCurrentColorGroup(QPalette::Disabled);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  18. The following user says thank you to jpn for this useful post:

    Apocalypse (31st March 2008)

  19. #17
    Join Date
    Mar 2008
    Location
    Ukraine, Donetsk
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle button hover

    That's it! Thank you!

Similar Threads

  1. question about button
    By narumi in forum Newbie
    Replies: 2
    Last Post: 21st January 2008, 05:44
  2. QTextBrowser and Hover
    By yogeshgokul in forum Qt Programming
    Replies: 1
    Last Post: 5th January 2008, 11:18

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.