Disable specific items in QComboBox
<p>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 conditions are met. </p>

<p>I indeed found someone did ask the same question here: <a href="https://stackoverflow.com/questions/11439773/disable-item-in-qt-combobox">Disable Item in Qt Combobox</a>
But none of these solutions in the answers seem to actually work (including the trick).</p>

<p>Is there a decent and 'correct' way to implement this?</p>

<p>EDIT:</p>

<p>I found out why setting the flags wouldn't disable the items in my application: for some reasons, I had to set the style <code>QStyle::SH_ComboBox_UseNativePopup</code>(see <a href="https://codereview.qt-project.org/#/c/82718/" rel="noreferrer">https://codereview.qt-project.org/#/c/82718/</a>). And this setting for some reasons blocked the flag setting. Does anyone have an idea why, and how to work around? A minimum test example is included (modified from the answer of @Mike):</p>

<pre><code>#include &lt;QApplication&gt;
#include &lt;QComboBox&gt;
#include &lt;QStandardItemModel&gt;
#include &lt;QProxyStyle&gt;

class ComboBoxStyle : public QProxyStyle
{
public:
    int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const override
    {
        if ( hint == QStyle::SH_ComboBox_UseNativePopup )
        {
            return 1;
        }
        return QProxyStyle::styleHint( hint, option, widget, returnData );
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QComboBox comboBox;

    // Setting this style would block the flag settings later on.
    comboBox.setStyle( new ComboBoxStyle() );

    comboBox.insertItem(0, QObject::tr("item1"));
    comboBox.insertItem(1, QObject::tr("item2"));

    QStandardItemModel* model = qobject_cast&lt;QStandardItemModel*&gt;(comboBox.model());
    QStandardItem* item= model-&gt;item(1);
    item-&gt;setFlags(item-&gt;flags() &amp; ~Qt::ItemIsEnabled);

    comboBox.show();
    return a.exec();
}
</code></pre>