Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-77141

Add a function to hide a QComboBox item

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Done
    • Not Evaluated
    • 5.15.2
    • None
    • Widgets: Itemviews
    • None
    • 360df2cf74410b05d1cab0192f2d0ff5a39db59e (qt/qtbase/5.15)

    Description

      Under some circumstances, it it desireable to hide a QComboBox item. E. g. if one wants to make a selection mandatory, one would like to add a dummy item which is selected by default but can't be selected by the user (like "choose one" or such), and as long as this one is selected, one can't e. g. press a "Proceed" button.

      Currently, this can be achieved, but in a quite inconvenient, non-obvious way. One has to do the following:

          // Cf. https://stackoverflow.com/questions/25172220/

          // Hide it in the popup
          QListView *view = qobject_cast<QListView *>(combo->view());
          Q_ASSERT(view != nullptr);
          view->setRowHidden(row, true);

          // Make it unaccessible for mouse wheel and arrow keys selection
          QStandardItemModel *model = qobject_cast<QStandardItemModel *>(combo->model());
          Q_ASSERT(model != nullptr);
          QStandardItem *item = model->item(row);
          item->setFlags(item->flags() & ~Qt::ItemIsEnabled);

      It would be nice if a QComboBox had a function like QComboBox::hideItem(int index) or such to do this.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              chehrlic Christian Ehrlicher
              l3u Tobias Leupold
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes