Results 1 to 3 of 3

Thread: Creating a style for a button

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Creating a style for a button

    Hi! I would like to create a button whose icon is changed according to its state, i.e. an icon is set if it is not focused and another when it has the focus. I wrote this in the drawControl method of a style I wrote:

    Qt Code:
    1. switch (element) {
    2. case QStyle::CE_PushButtonBevel: {
    3. // I have to draw the label.
    4. const QStyleOptionButton *buttonOpt = qstyleoption_cast<const QStyleOptionButton *>(option);
    5. QPushButton* buttonWidget = (QPushButton*)widget;
    6.  
    7. // Draw the correct icon.
    8. qDebug(QString::number(buttonOpt->state, 16).toStdString().c_str());
    9. if ((buttonOpt->state & QStyle::State_HasFocus) == QStyle::State_HasFocus)
    10. // Draw the default pixmap.
    11. painter->drawPixmap(buttonOpt->rect, buttonOpt->icon.pixmap(buttonWidget->size(), QIcon::Selected));
    12. else
    13. // Draw the pixmap to be used in case the widget is focused.
    14. painter->drawPixmap(buttonOpt->rect, buttonOpt->icon.pixmap(buttonWidget->size(), QIcon::Normal));
    15. break;
    16. }
    17. default:
    18. QWindowsStyle::drawControl(element, option, painter, widget);
    19. break;
    20. }
    To copy to clipboard, switch view to plain text mode 

    It seems it doesn't work and I'm quite far. I get two icons inside the button. Any idea how I can achieve what I need?
    Thanks!

  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: Creating a style for a button

    You probably don't need to customize the button class at all. Look at what QIcon offers - it allows you to set different pixmaps for different states.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Luc4 (23rd April 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Creating a style for a button

    Oh, now I see. I tried this before but I didn't understand it is necessary to set the state to QIcon::On. By default it is set to QIcon::Off. Very useful then! Thanks!

Similar Threads

  1. Creating Custom Ellipse Button
    By VireX in forum Qt Programming
    Replies: 18
    Last Post: 30th May 2012, 12:23
  2. QTabbar style ::scroller to expand the tab scoll button space.
    By AmolShinde_8 in forum Qt Programming
    Replies: 0
    Last Post: 22nd February 2010, 15:40
  3. Creating a button programmatically
    By Luc4 in forum Newbie
    Replies: 1
    Last Post: 27th January 2010, 11:30
  4. Problems with Gradient style used through Style sheet on ARM platform
    By puneet.narsapur in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 25th January 2010, 13:48
  5. Replies: 1
    Last Post: 7th February 2007, 01:12

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.