Hide entry of QComboBox/QAbstractItemView/QListView

  • Hi :-)

    I want to force the user to select an item of a QComboBox. So I added a "dummy" item as the first one, with -1 as data set. All "normal" item have data > 0. I check the selected data, and if it's still -1, nothing was selected yet.

    I subclassed the QComboBox, and first tried to make the dummy item not selectable, which is no problem. But it shows up in the popup. Although not being selectable, I'd rather like not to see it at all.

    Then I added a showPopup() override function. I checked if the first item has data -1 there, and if so, I deleted it. That made the dummy item vanish, but also caused the QComboBox to select the next one automatically.

    That doesn't look very good either – I would like to hide the dummy item in the popup. That seems to be quite hard … is this possible?

    Thanks for all help!

  • Lifetime Qt Champion

    Did you also try to set selected to -1 after you delete it in
    showPopup ?

  • @mrjj I tried this, but it makes the selected dummy entry disappear (nothing is selected) and also triggers a selection change. Simply not seeing that one item in the popup would be nice …

  • This post is deleted!

  • @mrjj I found a (surprisingly simple) solution (inside my QComboBox subclass):

    addItem(tr("–"), -1);
    qobject_cast<QListView *>(view())->setRowHidden(0, true);
    // Add other items

    The first item (my dummy item) is selected and shown as the default one, but the popup doesn't show it. Here we are :-)

Log in to reply