Disable Item in Qt Combobox

后端 未结 4 1689
孤街浪徒
孤街浪徒 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:54

    Taken from here:

    // Get the index of the value to disable
    QModelIndex index = ui.comboBox->model()->index(1, 0); 
    
    // This is the effective 'disable' flag
    QVariant v(0);
    
    // the magic
    ui.comboBox->model()->setData(index, v, Qt::UserRole - 1);
    

    To enable again use:

    QVariant v(1 | 32);
    

    The model used maps the flags word to Qt::UserRole - 1 -- that's what makes this code work. It's not a generic solution that would work with an arbitrary model.

提交回复
热议问题