Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why editable QComboBox changes its currentIndex when the first row of the model is inserted?

Why editable QComboBox changes its currentIndex when the first row of the model is inserted?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EmoBemo
    wrote on last edited by
    #1

    Here is a snippet from the Qt source where the index is changed:

    @void QComboBoxPrivate::_q_rowsInserted(const QModelIndex &parent, int start, int end)
    {
    Q_Q(QComboBox);
    if (inserting || parent != root)
    return;

    if (sizeAdjustPolicy == QComboBox::AdjustToContents) {
        sizeHint = QSize();
        adjustComboBoxSize();
        q->updateGeometry();
    }
    
    // set current index if combo was previously empty
    if (start == 0 && (end - start + 1) == q->count() && !currentIndex.isValid()) {
        q->setCurrentIndex(0); // Why ???
        // need to emit changed if model updated index "silently"
    } else if (currentIndex.row() != indexBeforeChange) {
        q->update();
        _q_emitCurrentIndexChanged(currentIndex);
    }
    

    }@

    Why would the current index need to be set to 0 when a row is inserted and the current index is not valid? If the combo is editable and the user has entered some text, when new row is inserted in the model the text entered by the user is lost. Is there a way to avoid such behaviour and just keep the entered or empty text?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      ksergey85
      wrote on last edited by
      #2

      My workaround:

      @QObject::connect(comboBox->model(), SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(saveComboBoxState()));
      QObject::connect(comboBox->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(restoreComboBoxState()));@

      and two private slots:

      @void myClass::saveComboBoxState()
      {
      comboBoxSavedIndex = comboBox->currentIndex();
      comboBoxSavedText = comboBox->currentText();
      }

      void myClass::restoreComboBoxState()
      {
      comboBox->setCurrentIndex(comboBoxSavedIndex);
      if (comboBox->isEditable())
      comboBox->setCurrentText(comboBoxSavedText);
      }@

      1 Reply Last reply
      0
      • E Offline
        E Offline
        EmoBemo
        wrote on last edited by
        #3

        Thanks a lot! I'll try this as soon as possible.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved