Disable Item in Qt Combobox

后端 未结 4 1712
孤街浪徒
孤街浪徒 2021-01-03 21:20

I can\'t find a standard way to disable an individual item in a Qt combo box. Is there a facility to do this in Qt that I am missing?

4条回答
  •  时光取名叫无心
    2021-01-03 21:48

    You can use the model of a QListWidget as a proxy.

    QComboBox *combo = new QComboBox(this);
    QListWidget *contents = new QListWidget(combo);
    contents->hide();
    combo->setModel(contents->model());
    
    /* Populate contents */
    contents->addItem("...");    // Etcetera
    

    Then, this method will disable an item:

    void disableItem(int index)
    {
        QListWidgetItem *item = contents->item(index);
        item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
    }
    

提交回复
热议问题