Вопрос

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
Это было полезно?

Решение

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

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

Другие советы

  1. Does tree->selectionModel() return a non-null pointer?

  2. How do you "remember" the selected items across application restarts?

  3. It is always an error for a model's client (anything that uses a model) to retain a QModelIndex after the model's structure was changed. Structural changes are any changes that add or remove items in the model. The isValid method does not detect this issue. Think of model indices as you would of standard C++ library iterators - they also become invalid when you add/remove items from a container.

    I don't know how you implement your model, but if you subclass/reuse any of the standard Qt models, this limitation also applies to your own use of the indices.

    The indices that survive structural changes are called persistent indices. You should use them instead, if you need such functionality.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top