qtbuttonqtoolbutton

QToolButton prevent from moving


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.


Solution

  • 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);