PDA

View Full Version : QItemSelectionModel::setCurrentIndex doesn't work



evgeniy
16th February 2014, 22:08
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!

ChrisW67
16th February 2014, 22:39
You cannot store QModelIndexes between sessions, they are create-use-destroy objects.

evgeniy
16th February 2014, 23:11
You cannot store QModelIndexes between sessions, they are create-use-destroy objects.
I know it. See qDebug. Read carefully: without "tree->selectionModel()->setCurrentIndex(ix, QItemSelectionModel::SelectCurrent);" works well...

ChrisW67
16th February 2014, 23:46
Read carefully: without "tree->selectionModel()->setCurrentIndex(ix, QItemSelectionModel::SelectCurrent);" works well...
So what is the problem again?


exited with code -1073741819
-1073741819 == 0xC0000005 Access Violation

Your debug output shows that (with reasonable likelihood) that the index points to an item in a valid model. It does not show that the index is related in any way to the model currently displayed in the tree, but I am willing to assume that it is because that would not lead to a crash anyway. It does not show that tree is a valid pointer. It does not show that tree->selectionModel() is not 0. Either pointer being broken seems reasonably possible given the outcome. Exactly which line is your debugger telling you the crash occurred at? What is the backtrace to that point? What is the value of tree at this point? What does tree->selectionModel() return at that point?

evgeniy
17th February 2014, 00:03
tree->selectionModel()->clear(); works
tree->setExpanded(ix.parent(), true); works
tree->scrollTo(ix); works
Could you tell me how to get backtrace from qt creator/gdb?

ChrisW67
17th February 2014, 00:17
The "bt" gdb command:


$ gdb ./mydebugexe
(gdb) run
...
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ac5ba7 in ?? () from /lib64/libc.so.6
(gdb) bt
#0 0x00007ffff7ac5ba7 in ?? () from /lib64/libc.so.6
#1 0x0000000000400597 in test () at main.cpp:6
#2 0x00000000004005ad in main (argc=1, argv=0x7fffffffdae8) at main.cpp:10


If you are using gdb inside Qt Creator then you get a backtrace automatically.

evgeniy
17th February 2014, 00:26
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

evgeniy
20th February 2014, 12:20
It's a Qt bug. The only way out:

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