Results 1 to 9 of 9

Thread: QToolButton and Style sheet

  1. #1
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question QToolButton and Style sheet

    Hi everybody,

    Description

    I have a QAction with an icon
    the QAction is set in Qmenu with
    Qt Code:
    1. menu->addAction(myAction)
    To copy to clipboard, switch view to plain text mode 

    on a widget inside the application, I have a QToolButton with the following code
    Qt Code:
    1. myQToolButton->setDefaultAction(myAction)
    To copy to clipboard, switch view to plain text mode 
    this is done to have different access to the same action in different place

    the style sheet is the following:

    Qt Code:
    1. QToolButton#myQToolButton{image: url(:/image.png);}
    2. QToolButton#myQToolButton:hover{image: url(:/image_rollover);}
    3. QToolButton#myQToolButton:pressed{image: url(:/image_pushed);}
    4. QToolButton#myQToolButton:disabled{image: url(:/image_disabled);}
    To copy to clipboard, switch view to plain text mode 


    so the first question:
    In understand that the QtoolButtoin is connected with the action but
    how to remove the icon in QtoolButton to set correctly the style sheet inside?

    So my second question relative to the first question:
    If it's not possible to remove icon, How to set the style to an action inside a QMenu in order to get the same style to the Qmenu AND to the ToolButton


    Thanks to all

    David

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QToolButton and Style sheet

    Do you have to use the stylesheet? Can't you use what QIcon offers?

  3. #3
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QToolButton and Style sheet

    That's not so easy .... The whole application is based on Style sheet; some buttons change their pictures when they are toggled... others get differents states with different icones

    it's more beautiful ... yes I know not very useful...


    Anyway if Qicon can make all states in rollover, pressed , disabled, and so an tell me, I will code for that ; but could you tell me how to do that...

    Thanks

    pozdrowienia !

    David

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QToolButton and Style sheet

    Take a look at QIcon::addPixmap and its parameters. You can add different pixmaps for different icon states.

  5. #5
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QToolButton and Style sheet

    I test it in different case
    but it seems that's not working for the "pushed mode" on not checkable buttons;

    this is a property we use constantly, and it sems We should subclass mouseMoveEvent for a QToolButton and install and Event Filter

    Somethings I don't want for just setting icones.

    If I go on with style sheet, is there a way to subclass paintEvent in order to remove the iCon inside the QtoolButton and let go the style sheet through the QToolButton ,
    it could be done, no ???

    Thanks for your help

    David

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QToolButton and Style sheet

    If you override paintEvent, you don't have to use stylesheets but instead do your completely custom painting based on your own set of icons.

  7. #7
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QToolButton and Style sheet

    Ok , could you just post a little piece of code about that

    Thanks

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QToolButton and Style sheet

    Qt Code:
    1. void X::paintEvent(QPaintEvent *pe){
    2. QPixmap px;
    3. if(...){
    4. px = QPixmap("p1.png");
    5. } else if(...){
    6. px = QPixmap("p2.png");
    7. } else ...
    8. QPainter p(this);
    9. p.drawPixmap(rect(), px);
    10. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: [SOLVED]QToolButton and Style sheet

    I find exactly What I have to do;

    state are : Normal, Rollover, Pushed/on, Disabled
    state not implemeneted are pushed mode.

    In order to get an icone for different state, independant from the style

    I do that:
    Qt Code:
    1. QIcon icon;
    2. icon.addFile(":/Normal.png", QSize(), QIcon::Normal, QIcon::Off); // Normal
    3. icon.addFile(":/Rollover.png", QSize(), QIcon::Active, QIcon::Off); //rollover
    4. icon.addFile(":/Pushed.png", QSize(), QIcon::Active, QIcon::On); //pushed/On
    5. //disabled is already implemented
    6.  
    7. _myAction->setIcon(icon);
    8. myToolButton->setDefaultAction(_undoAct);
    To copy to clipboard, switch view to plain text mode 


    In a subclass of QtoolButton, I override the paintEvent like that:

    Qt Code:
    1. void CAdvToolButton::paintEvent(QPaintEvent * e)
    2. {
    3. // case for pushed mode
    4. if (isEnabled() && isDown())
    5. {
    6. QStylePainter p(this);
    7. initStyleOption(&opt);
    8.  
    9. // modify style Option: "icon", to get a new icon inside
    10. opt.icon = QIcon(icon().pixmap(iconSize(), QIcon::Active, QIcon::On));
    11.  
    12. p.drawComplexControl(QStyle::CC_ToolButton, opt);
    13. }
    14. else
    15. QToolButton::paintEvent(e); // all other mode are supported
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    that'all

    David

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.