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

QComboBox (Editable) adds item to wrong index when rootModelIndex is set

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.8.0 Beta
    • None
    • I have been working on Linux desktop, but I don't think this code is OS dependent - its a logical Qt internal bug, not an issue with the environment.

    Description

      When QComboBox is editable, and using it with a hierarchical model (QStandardItemModel), and setting the root index with setRootModelIndex() to show a specific leaf of the model.
      (This probably also happens with non editable QComboBox which has setRootModelIndex() other than the model root item)
      Typing a new item in the editline and pressing enter adds the new item to the model, as one would expect.
      However, one also would expect the item will be added to the leaf the QComboBox is showing, that is, as a child of the root index the combo box is set with - this however is NOT what happens.
      The new item gets added to the root of the model it self.
      I have looked at the code, the bug is in QComboBox::insertItem():

      qcombobox.cpp borderStyle=solid
      // For the common case where we are using the built in QStandardItemModel
           // construct a QStandardItem, reducing the number of expensive signals from the model
           if (QStandardItemModel *m = qobject_cast<QStandardItemModel*>(d->model)) {
               QStandardItem *item = new QStandardItem(text);//<<<<< BUG part 1
               if (!icon.isNull()) item->setData(icon, Qt::DecorationRole);
               if (userData.isValid()) item->setData(userData, Qt::UserRole);
               m->insertRow(index, item); //<<<< BUG part 2
               ++itemCount;
           }
      

      This way of inserting a new item totally ignores the fact that QComboBox offers the ability to work with hierarchical models via setRootModelIndex(), and thus all the new items always end up as children of the model's root instead of the item combo box is using as root index.
      I have patched it locally and it works well for me with the above scenario and as far as I could tell it still supports the old behavior as well, see attached patch.
      Basically, the patch gets the item from the current root index the combo box has set - if its a valid one, it gets the corresponding item as parent for the new item, if not, it gets the invisibleRootItem() as parent for the new item, which is what (implicitly) happens in the original code.

      Attachments

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

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            daniel.kish daniel.kish
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes