尊重样式的同时从禁用的QComboBox中删除箭头

时间:2018-11-27 16:42:59

标签: c++ qt qcombobox

在下面的图片中,第一个QComboBox被禁用:

enter image description here

我想强调一个事实,即不能通过删除禁用的QComboBox es的箭头来更改值。

我尝试修改已经用于的样式表:

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

但是它不能解决问题,并且与我当前的风格(使用qApp->setStyle("fusion")设置)冲突:

enter image description here

我如何得到它?

1 个答案:

答案 0 :(得分:5)

可以通过使用QProxyStyle并为箭头子控件(QProxyStyle::subControlRect)返回空QRect来完成技巧。 QProxyStyle允许您更改某种样式的特定行为,而无需实现一个全新的样式(它包装了原始样式)。

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

结果:

enter image description here

相关问题