Disable specific items in QComboBox

后端 未结 2 1805
轻奢々
轻奢々 2020-12-16 17:09

In my application, I want to disable some items (i.e. not selectable, no highlights when mouse hovering above, and the texts are greyed out) in the QComboBox when certain co

2条回答
  •  庸人自扰
    2020-12-16 17:42

    Here is the technique @Mike describes, wrapped up in a convenient utility function:

    void SetComboBoxItemEnabled(QComboBox * comboBox, int index, bool enabled)
    {
        auto * model = qobject_cast(comboBox->model());
        assert(model);
        if(!model) return;
    
        auto * item = model->item(index);
        assert(item);
        if(!item) return;
        item->setEnabled(enabled);
    }
    

提交回复
热议问题