2

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?

1 Answer 1

5

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

2
  • Any chance you could write this in Python? Sep 8, 2022 at 3:15
  • @somethingvague this post is focused on C++ but you can implement it quite straightforward in Python following doc.qt.io/qtforpython/PySide6/QtWidgets/QProxyStyle.html. Regarding the other parameters, the combo-box arrow sub-control in Python is QtWidgets.QStyle.SC_ComboBoxArrow for example. Hope it can help; if not, please feel free to open a new question (you can link to this one as reference if it helps you)
    – cbuchart
    Sep 8, 2022 at 10:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.