Change ToolButton image programatically



  • I'm building a system that will eventually run on a BeagleBone Black. Right now I'm doing my development on Kubuntu using QT4.6.5 & QTCreator 2.6.2.

    I'm trying to use toolbuttons as a radiobutton with an image. In my case the button state is set by the program and there is no user interaction to change the state, this happens from system state changes. What I would like to do is be able to set a parameter to change the displayed image. I'm specifying the image in a style sheet like this:
    @

    • {
      border: none;
      background-image: url(:/MainScreen/images/SnowOff.gif);
      }
      pressed {
      border: none;
      background-image: url(:/MainScreen/images/SnowOn_animated.gif);
      }
      @

    I starting by just specifying the images in the AbstractButton, but couldn't get the look that I wanted, so I changed to a style sheet.

    I've tried changing the down state and also the clicked state, but the only way that I've been able to actually change the image is by calling setIcon() which seems a bit heavy handed, and bypasses the style sheet.

    Am I missing something?



  • I just realized that I can replace the image on a radio button and I've tried that but calling clicked() on the radio button still doesn't change the image.

    I'm obviously missing something.



  • I encountered similar issues some time ago. Ended up with a new class (class AsButton : public QPushButton) which loaded the images and reimplemented the paint event.



  • I think I've solved my problem, although I didn't answer my original question.

    I converted my state buttons to radio buttons, each with its own widget wrapper so that they are independent of each other. Then the setChecked() call properly sets the image that I want displayed. This is what I should have attempted first, had I figured out that the radio button allowed changing the graphic.

    It would be nice if the Check Box allowed changing the graphic, then I wouldn't have to have the extra widget wrappers around each of the state radio buttons.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.