2

I am attempting to make the arrow buttons on a spinbox appear disabled when the value in the spin box is at either the maximum or minimum.

I am using a QStyleSheet which contains:

QAbstractSpinBox::up-arrow:off, QAbstractSpinBox::up-arrow:disabled {
    background: #131313;
}

However, the 'off' pseudo state is not being set when I set the spinbox to the maximum value. Thus, this style is never being applied.

I have tried:

  • Using QSpinBox, QDoubleSpinBox selectors as above
  • setting other properties such as width and height

I know the style is reading correctly because if I disable the SpinBox altogether, this styling shows up.

Any ideas?

Ryan S.
  • 21
  • 3

2 Answers2

0

Most spin boxes are directional, but QSpinBox can also operate as a circular spin box, i.e. if the range is 0-99 and the current value is 99, clicking "up" will give 0 if wrapping() is set to true. Use setWrapping() if you want circular behavior.

  • Wrapping is not enabled. I want the up-arrow button to show up disabled when the value is at the maximum. The Qt Documentation says the :off pseudo-state in the QSpinBox styles does this, but I haven't had any success. – Ryan S. May 20 '21 at 17:20
0

You have to turn on QStyle::SH_SpinControls_DisableOnBounds:

#include <QApplication>
#include <QProxyStyle>

class MyProxyStyle : public QProxyStyle
{
  public:
    int styleHint(StyleHint hint, const QStyleOption *option = nullptr,
                  const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
    {
        if (hint == QStyle::SH_SpinControls_DisableOnBounds)
            return 1;
        return QProxyStyle::styleHint(hint, option, widget, returnData);
    }
};

int main(int argc, char **argv)
{    
    QApplication a(argc, argv);
    a.setStyle(new MyProxyStyle);
    // ...
}
Osama
  • 324
  • 3
  • 11