Search code examples
qtqtreeview

QItemSelectionModel::setCurrentIndex doesn't work on opening


My code:

void TreeModel::selectIndex(QModelIndex ix) {
  if (!ix.isValid()) return;
  qDebug() << "name1" << ix.data();
  tree->selectionModel()->clear();
  tree->setExpanded(ix.parent(), true);
  tree->selectionModel()->setCurrentIndex(ix, QItemSelectionModel::SelectCurrent);
  tree->scrollTo(ix);
}

This snippet runs ok after deleting/inserting nodes. But when I try to select the remembered node from previous session in QMainWindow::showEvent the result is:

name1 QVariant(QString, "Highlight elements") 
The program has unexpectedly finished.
C:\Home\develop\qt\arm\designer\Designer exited with code -1073741819

without tree->selectionModel()->setCurrentIndex(ix, QItemSelectionModel::SelectCurrent); works well, but I do need to select this item.

Thank you very much in advance!

debugger log:

>~"\nProgram received signal "
>~"SIGSEGV, Segmentation fault.\n"
>~"0x00438290 in QVariant::Private::Private (this="
>&"warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)\n"
 (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
>&"\n"
>~"0x0) at ../../../../../../Qt/Qt5.0.2/5.0.2/mingw47_32/include/QtCore/qvariant.h:367\n"
>~"367\t        inline Private(): type(Invalid), is_shared(false), is_null(true)\n"

>~"data=[{iname=\"local.node\",name=\"node\",addr=\"0x14384220\",addr=\"0x14384220\",numchild=\"9\",origaddr=\"0x28d86c\",type=\"DNode\",value=\"{...}\",},{iname=\"local.this\",name=\"this\",addr=\"0x14341820\",addr=\"0x14341820\",numchild=\"5\",origaddr=\"0x28d890\",type=\"TreeModel\",value=\"{...}\",},{iname=\"local.index\",name=\"index\",addr=\"0x28dba8\",numchild=\"0\",type=\"QModelIndex &\",value=\"(invalid)\",},{iname=\"local.role\",name=\"role\",addr=\"0x28d898\",numchild=\"0\",type=\"int\",value=\"1\",},],typeinfo=[{name=\"aW50\",size=\"4\"}{name=\"Y29uc3QgUU1vZGVsSW5kZXggJg==\",size=\"4\"}{name=\"RE5vZGUgKg==\",size=\"4\"}{name=\"RE5vZGU=\",size=\"36\"}]\n"
>2390^done
dDISCARDING JUNK AT BEGIN OF RESPONSE: 
dProgram received signal SIGSEGV, Segmentation fault.
d0x00438290 in QVariant::Private::Private (this=0x0) at ../../../../../../Qt/Qt5.0.2/5.0.2/mingw47_32/include/QtCore/qvariant.h:367
d367            inline Private(): type(Invalid), is_shared(false), is_null(true)
 <Rebuild Watchmodel 243>
sFinished retrieving data

Solution

  • It's a Qt bug. The only way out:

    void MainWindow::showEvent(QShowEvent *event) {
      QMainWindow::showEvent(event);
      QTimer::singleShot(0, this, SLOT(selectLastNode()));
    }