Search code examples
c++qtqcombobox

Remove arrow from disabled QComboBox while respecting style


In the picture below, the first QComboBox is disabled:

enter image description here

I want to emphasize the fact that the value cannot be changed by removing the arrow of disabled QComboBoxes.

I've tried modifying the stylesheet already being used to:

QComboBox::down-arrow:disabled {
  border: 0;
  background: transparent;
  image: none;
  height: 0;
  width: 0;
}

But it doesn't solve the issue and conflicts with my current style (set using qApp->setStyle("fusion")):

enter image description here

How can I get it?


Solution

  • The trick can be done by using a QProxyStyle and returning a null QRect for the arrow subcontrol (QProxyStyle::subControlRect). A QProxyStyle allows you to vary specific behaviours of a style without the need of implementing a whole new one (it wraps the original style).

    class MyProxyStyle : public QProxyStyle {
    public:
      MyProxyStyle(const QString& base_style_name) : QProxyStyle(base_style_name) {}
    
      QRect MyProxyStyle::subControlRect(QStyle::ComplexControl cc,
                                         const QStyleOptionComplex* option,
                                         QStyle::SubControl sc,
                                         const QWidget* widget) const override
      {
        if (cc == CC_ComboBox && sc == SC_ComboBoxArrow && !widget->isEnabled()) return QRect();
        return QProxyStyle::subControlRect(cc, option, sc, widget);
      }
    };
    
    // ...
    
    qApp->setStyle(new MyProxyStyle("fusion"));
    

    Result:

    enter image description here