Results 1 to 4 of 4

Thread: Change button Icon on toggle

  1. #1
    Join Date
    Jul 2009
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Change button Icon on toggle

    Hello, all;

    I'm trying to program a QPushButton or QToolButton to change the icon being displayed when toggled/activated. Unfortunately, I'm on a shard system and unable to use QSetIcon or add it to the library. Any suggestions on how to proceed?

    Thanks in advance,

    Allan

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change button Icon on toggle

    Probably you want to use QIcon class . You can find example how to use it with QPushButton in Assistant.

    If you want to toggle the icon, you should connect signal clicked() or toggled() of your button to your custom slot and in that slot just call QPushButton::setIcon() method.

    Again, read about QPushButton and QIcon in Assistant.

    If your button is checkable you can also do it without signal and slot. Here's an example:
    Qt Code:
    1. QToolButton *b = new QToolButton(this);
    2. QIcon *ico = new QIcon();
    3. ico->addPixmap(QPixmap("on.jpg"),QIcon::Normal,QIcon::On);
    4. ico->addPixmap(QPixmap("off.jpg"),QIcon::Normal,QIcon::Off);
    5. b->setIcon(*ico);
    6. b->setCheckable(true);
    To copy to clipboard, switch view to plain text mode 


    Unfortunately, I'm on a shard system and unable to use QSetIcon or add it to the library
    I really don't understand what do you mean. Could you elaborate?
    Last edited by calhal; 13th July 2009 at 21:32. Reason: updated contents
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

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

    bizmopeen (13th July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Change button Icon on toggle

    Quote Originally Posted by calhal View Post
    I really don't understand what do you mean. Could you elaborate?
    Sorry, that was supposed to read "I'm on a shared system". (Fat Fingers strikes again...) Basically, the documentation for QSetIcon mentions the need for the "qseticon.h" header file, which I can't add to the library because I'm not the Administrator and don't have access. In any event, I will try the method you posted above, many thanks!

  5. #4
    Join Date
    Jul 2009
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Change button Icon on toggle

    Success! Thank you very much, calhal.

Similar Threads

  1. How to change text color of push button?
    By augusbas in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2009, 10:32
  2. Replies: 7
    Last Post: 31st May 2009, 11:30
  3. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22:25
  4. button with backgr and icon using stylesheets
    By s_p_t10 in forum Qt Programming
    Replies: 0
    Last Post: 7th May 2008, 20:19
  5. Icon Change in Drag & Drop
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2006, 12:03

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.