0

Is there a way to prevent QToolButton from being "pressed in" when clicked? I read somewhere that setting

button->setCheckable(false);

should do the trick, but it doesn't.

madasionka
  • 812
  • 2
  • 10
  • 29
  • You should draw a button. When it is in pressed state you should draw it as normal. Just copy-pase paint method and fix it. Or use QSS to draw pressed button in same way as normal. – Dmitry Sazonov Nov 24 '17 at 09:04

2 Answers2

3

There is a way to do it via a QProxyStyle:

class ButtonProxyStyle : public QProxyStyle
{
public:
    const int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) 
    {
        int ret = 0;
        switch (metric) 
        {
        case QStyle::PM_ButtonShiftHorizontal:
        case QStyle::PM_ButtonShiftVertical:
            ret = 0;
            break;
        default:
            ret = QProxyStyle::pixelMetric(metric, option, widget);
            break;
        }
        return ret;
    }
};

And then, with your button:

myToolButton->setStyle(new ButtonProxyStyle);
IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
  • Thanks! I actually tried this solution and it didn't work, now that you suggested it I tried again and investigated more. I think I had a problem with this because I also set `myToolButton->setStyleSheet("QToolButton {border: none; color: #223f5a;}");` And I don't think those two styles go along. Do you maybe know how to integrate this style sheet into the `QProxyStyle` ? – madasionka Nov 24 '17 at 09:21
  • You're right: stylesheets and proxy styles don't work together. If you use my solution, you will have to paint your button from scratch, [which is a bit tedious](http://doc.qt.io/qt-5/style-reference.html). Maybe the best solution for you is to subclass QAbstractButton and give the behaviour you want? – IAmInPLS Nov 24 '17 at 09:40
0

Add QAction to the toolbar and use it to controll your tool button

// button action
QAction * poBtnAction = poToolbar->addWidget(button);
// disable button
poBtnAction->setEnabled(false);
Simon
  • 1,522
  • 2
  • 12
  • 24
  • Thanks for answering, but I don't have a toolbar. I Just have a QToolButton. Basically what I am doing is customizing this : https://github.com/Elypson/qt-collapsible-section. – madasionka Nov 24 '17 at 08:29