Results 1 to 11 of 11

Thread: Using ICONs as PushButtons

  1. #1
    Join Date
    Jan 2010
    Location
    Bangalore, India
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Using ICONs as PushButtons

    I am developing a simple GUI application using Qt 4.5 (on a windows PC).

    I wanted to make a cute push button using my own icons.

    I tried using the QPushButton class and a simple image as a pixmap. I could always see a rectangular outline around the icon, whenever it was clicked.
    How to do away with that.

    I also created a QIcon object with two icons (*.png files) one for the state: "Normal" and the other for the state: "Selected". (Refer the attachments).
    But couldnt get the clue on how to link the push button states to these icons.

    So, now my questions are:
    1. How to use image files like *.png, to create a custom push button (without getting the rectangular outline)
    2. Is it always a must to use QPushButton class for this, OR do we have an alternative for realizing the above?

    - Sudhakaran
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using ICONs as PushButtons

    1. How to use image files like *.png, to create a custom push button (without getting the rectangular outline)
    see setMask().

    2. Is it always a must to use QPushButton class for this, OR do we have an alternative for realizing the above?
    nothing is a must.
    The questions is, what functionality so you need, which QPushButton does not offer you?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Using ICONs as PushButtons

    QAbstractButton has the icon property, which holds a QIcon variable. Take a look at QIcon documentation - This class allows you to specify separate images for different icon states.

  4. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using ICONs as PushButtons

    Try using a QToolButton instead of a QPushButton.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Using ICONs as PushButtons

    You also can use a simple QWidget as a base class and then reimplement mouse press events and emit a clicked signal by your own...

  6. #6
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using ICONs as PushButtons

    Take a look at QIcon documentation - This class allows you to specify separate images for different icon states.
    How can I specify different images for different modes in QIcon? I've seen that there is an enum "Mode" to identify the state, but I don't see how to add different images to the Icon representing the modes ...
    Last edited by BenPa; 4th February 2010 at 10:02. Reason: Added quote ..

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using ICONs as PushButtons

    You can connect a slot to the button, check the state, and set the correct icon for the given state.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using ICONs as PushButtons

    Quote Originally Posted by high_flyer View Post
    You can connect a slot to the button, check the state, and set the correct icon for the given state.
    But for what do I need the "Mode" enum defined in the QIcon class? I don't see a function to set a mode for a QIcon. Using different modes in a icon makes sense to show different states, but if I have to implement this functionality myself, I don't understand for what the mode is defined in the class..

    Thank you for the tip .

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using ICONs as PushButtons

    Its all in the QIcon documentation.
    Qt can generate disabled/selected icons automatically using widely used conventions.
    If these conventions suit you, you can certainly use these auto generated icons.
    QPushButton will show the icons correctly depending on the state.

    But your case is different, since you want to use different images for various states, so you have to implement it your self.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using ICONs as PushButtons

    Quote Originally Posted by BenPa View Post
    How can I specify different images for different modes in QIcon?
    Use QIcon::addFile or QIcon::addPixmap.

    If your use fits within the paradigm of four modes (normal, disabled, active and selected) and two states (on and off), you can use this method to specify up to eight images for one QIcon. If that matrix does not reasonably represent the set of images you need, then you’ll need to develop your own method of switching images; you could do that in various ways, such as by re-implementing QWidget::paintEvent or by calling QAbstractButton::setIcon whenever the correct image to display changes.

  11. #11
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using ICONs as PushButtons

    Quote Originally Posted by BenPa View Post
    I don't see a function to set a mode for a QIcon.
    You don’t set the mode of a QIcon; you choose the desired mode and state when you paint the icon with QIcon::paint or retrieve an image with QIcon::pixmap. When using a class derived from QAbstractButton, the button takes care of painting the appropriate image depending on the mode and state of the button, so you can add all the images to the icon and then be concerned only with the mode and state of the button, without thinking any further about the icon.

Similar Threads

  1. Three pushButtons in a shape of one
    By Kenji_Takahashi in forum Qt Programming
    Replies: 13
    Last Post: 7th September 2009, 19:25
  2. Where to get Icons from
    By pospiech in forum General Discussion
    Replies: 4
    Last Post: 19th December 2008, 21:14
  3. Pushbuttons with fixed aspect ratios?
    By WinchellChung in forum Newbie
    Replies: 3
    Last Post: 19th December 2007, 18:38
  4. Problems connecting PushButtons
    By Randulf in forum Newbie
    Replies: 3
    Last Post: 23rd August 2006, 14:39
  5. (Qt 4) QTreeview and Icons?
    By pinktroll in forum Qt Programming
    Replies: 4
    Last Post: 17th August 2006, 16:06

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.