Important: Please read the Qt Code of Conduct -

[SOLVED] QToolButton, QPushButton, style sheets and disabling

  • I've got a ui with dialog window and some checkable buttons. For buttons I used a QToolButton class and made them icon with text beside icon. So I need to disable clicks on some of them, but if I use a set them disabled the icon will be gray-scale bitmap. I have some ideas like to use style sheet "border-style: outset; border-width: 2px; border-color: red; " and border-style: outset; border-width: 2px; border-color: blue; " to visualize difference between disabled and enabled buttons. The next step to disable clicking on red buttons. I think about get button click() signal and do reverse click or to get mouse click event and do the click if cursor on blue button and not on red(in this way I need to some list which stores my red button object names). So the question is obvious: "Is there any other simple way to solve this problem ?"

  • You can make the visual distinction between your enabled and disabled buttons using the stylesheet. However, instead of changing the behaviour of the widget based on the colors you set, you should do the reverse: craft your stylesheet so it uses the right styles for the enabled and for the disabled states. The Qt stylesheet system gives you a load of different pseudo states to do that, among them the :disabled and the :enabled pseudo-states. You can use these in your selectors for the style.

    Then, you can just enable or disable the button, and the visual appearance will follow suit. Note that if you don't like the grey image on disabled buttons, you can set your own image by setting an appropriate image for the disabled mode in the QIcon you set for the button.

  • soemwhere, you set the QIcon. You can set different *.png files for different states on a "QIcon": .

    like this:
    QIcon ico;
    ico.addPixmap(QPixmap(":/on.png"), QIcon::Normal);
    ico.addPixmap(QPixmap(":/on.png"), QIcon::Disabled);

  • Thanks a lot Gerolf it is the simplest way. I forgot that I'm using ui file :)

Log in to reply