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

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

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

    if (sizeAdjustPolicy == QComboBox::AdjustToContents) {
        sizeHint = QSize();
    // 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) {


    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?

  • 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()
    if (comboBox->isEditable())

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.