QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 )
-
Dear Forum,
I created a QTreeView which I can fill with several items as children of rootItem. When I then insert a child to the first child of the tree (so the child is root->(0,0)->(0,0)), I get an error message on the screen:
QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ) in model QAbstractItemModel(0x5564d96a0150)When I insert other children, e.g. root->(0,0)->(1,0) or root->(1,0)->(0,0), this error does not appear. After inserting the child, I select the newly created row. That also works for all other children, but not for root->(0,0)->(0,0). There, I end up with a "half-way" selection (see picture): the child is "a bit selected" but the parent row, root->(0,0), stays fully selected. Is that a bug or a my mistake?

The relevant code pieces areQModelIndex TreeModel::addKurs(Kurs* k) { QModelIndex idx = index(k->semester()); beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount()); k->semester()->addKurs(k); endInsertRows(); emit(layoutChanged()); QModelIndex idx2 = createIndex(k->row(), 0, k); return idx2; }void HagenForum::rightClickTree() { TreeItem *source = treeModel()->treeItem(treeView()->currentIndex()); if (source->type() == TreeItem::Type::Semester) { Semester *sem = reinterpret_cast<Semester*>(source); SemDialog sd(sem, this); if (sd.exec() == QDialog::DialogCode::Accepted) { QModelIndex idx = treeModel()->addKurs(sd.kurs()); QTreeView *tv = treeView(); //must select with delay, otherwise parent Semester will be selected QTimer::singleShot(100, [tv, idx]() { tv->setCurrentIndex(idx); }); }; } }I double checked that k->semester()->childCount is correct (0 before insertion, 1 after insertion). Where does that illegal index (2,0) come from??
Any help is appreciated!
-
Dear Forum,
I created a QTreeView which I can fill with several items as children of rootItem. When I then insert a child to the first child of the tree (so the child is root->(0,0)->(0,0)), I get an error message on the screen:
QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ) in model QAbstractItemModel(0x5564d96a0150)When I insert other children, e.g. root->(0,0)->(1,0) or root->(1,0)->(0,0), this error does not appear. After inserting the child, I select the newly created row. That also works for all other children, but not for root->(0,0)->(0,0). There, I end up with a "half-way" selection (see picture): the child is "a bit selected" but the parent row, root->(0,0), stays fully selected. Is that a bug or a my mistake?

The relevant code pieces areQModelIndex TreeModel::addKurs(Kurs* k) { QModelIndex idx = index(k->semester()); beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount()); k->semester()->addKurs(k); endInsertRows(); emit(layoutChanged()); QModelIndex idx2 = createIndex(k->row(), 0, k); return idx2; }void HagenForum::rightClickTree() { TreeItem *source = treeModel()->treeItem(treeView()->currentIndex()); if (source->type() == TreeItem::Type::Semester) { Semester *sem = reinterpret_cast<Semester*>(source); SemDialog sd(sem, this); if (sd.exec() == QDialog::DialogCode::Accepted) { QModelIndex idx = treeModel()->addKurs(sd.kurs()); QTreeView *tv = treeView(); //must select with delay, otherwise parent Semester will be selected QTimer::singleShot(100, [tv, idx]() { tv->setCurrentIndex(idx); }); }; } }I double checked that k->semester()->childCount is correct (0 before insertion, 1 after insertion). Where does that illegal index (2,0) come from??
Any help is appreciated!
@Stephan-Lorenzen said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):
Where does that illegal index (2,0) come from??
Don't know but I would set a breakpoint on QAbstractItemModel::endInsertRows() and see where it really comes from. What does e.g. k->semester()->addKurs(k); do?
-
Dear Forum,
I created a QTreeView which I can fill with several items as children of rootItem. When I then insert a child to the first child of the tree (so the child is root->(0,0)->(0,0)), I get an error message on the screen:
QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ) in model QAbstractItemModel(0x5564d96a0150)When I insert other children, e.g. root->(0,0)->(1,0) or root->(1,0)->(0,0), this error does not appear. After inserting the child, I select the newly created row. That also works for all other children, but not for root->(0,0)->(0,0). There, I end up with a "half-way" selection (see picture): the child is "a bit selected" but the parent row, root->(0,0), stays fully selected. Is that a bug or a my mistake?

The relevant code pieces areQModelIndex TreeModel::addKurs(Kurs* k) { QModelIndex idx = index(k->semester()); beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount()); k->semester()->addKurs(k); endInsertRows(); emit(layoutChanged()); QModelIndex idx2 = createIndex(k->row(), 0, k); return idx2; }void HagenForum::rightClickTree() { TreeItem *source = treeModel()->treeItem(treeView()->currentIndex()); if (source->type() == TreeItem::Type::Semester) { Semester *sem = reinterpret_cast<Semester*>(source); SemDialog sd(sem, this); if (sd.exec() == QDialog::DialogCode::Accepted) { QModelIndex idx = treeModel()->addKurs(sd.kurs()); QTreeView *tv = treeView(); //must select with delay, otherwise parent Semester will be selected QTimer::singleShot(100, [tv, idx]() { tv->setCurrentIndex(idx); }); }; } }I double checked that k->semester()->childCount is correct (0 before insertion, 1 after insertion). Where does that illegal index (2,0) come from??
Any help is appreciated!
Which line produces the error message? Where does it stop?
One thing I've noticed, is that you have this block:
beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount()); k->semester()->addKurs(k); endInsertRows();The documentation states:
An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and endInsertRows() immediately afterwards.
If
addKurs()works as yourinsertRows()implementation, the line betweenbeginandendis invalid or may cause issues.
I'm wondering why you use this begin and end functions, when you dont implementinsertRows().I really dont understand, what you are doing anyway. Everything looks a bit "fishy"...
Especially the line betweenbeginandend:
k->semester()->addKurs(k); -
Hi Christian,
k->semester()->addKurs(Kurs *k) just appends the new child to the vector of children.
What do you mean with a breakpoint, some debugger examining that part of the code? That's kind of embarassing, but I have no experience with debuggers, I usually just insert hundreds of
std::cerr << "I am line 467 of gobbledigoo()" << std::endl;in my code for that purpose...
I just found out that the same behavior also appears "one level below", i.e. if I add root->(0,0)->(0,0)->(0,0), I get error messages
QAbstractItemModel::endInsertRows: Invalid index ( 1 , 2 ) in model QAbstractItemModel(0x557156028710) QAbstractItemModel::endInsertRows: Invalid index ( 1 , 0 ) in model QAbstractItemModel(0x557156028710)other "third generation"-children behave as they should.
-
Which line produces the error message? Where does it stop?
One thing I've noticed, is that you have this block:
beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount()); k->semester()->addKurs(k); endInsertRows();The documentation states:
An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and endInsertRows() immediately afterwards.
If
addKurs()works as yourinsertRows()implementation, the line betweenbeginandendis invalid or may cause issues.
I'm wondering why you use this begin and end functions, when you dont implementinsertRows().I really dont understand, what you are doing anyway. Everything looks a bit "fishy"...
Especially the line betweenbeginandend:
k->semester()->addKurs(k);@Pl45m4 said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):
Which line produces the error message? Where does it stop?
e.g. with QT_FATAL_WARNINGS or qInstallMessageHandler() or search in the Qt sources and set the breakpoint directly there.
-
Which line produces the error message? Where does it stop?
One thing I've noticed, is that you have this block:
beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount()); k->semester()->addKurs(k); endInsertRows();The documentation states:
An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and endInsertRows() immediately afterwards.
If
addKurs()works as yourinsertRows()implementation, the line betweenbeginandendis invalid or may cause issues.
I'm wondering why you use this begin and end functions, when you dont implementinsertRows().I really dont understand, what you are doing anyway. Everything looks a bit "fishy"...
Especially the line betweenbeginandend:
k->semester()->addKurs(k);@Pl45m4 said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):
If addKurs() works as your insertRows() implementation, the line between begin and end is invalid or may cause issues.
I'm wondering why you use this begin and end functions, when you dont implement insertRows().Then I do not quite understand the concept of Qt. I looked at some examples, and insertRows() usually does nothing besides calling beginInsertRows(), appending something to a QVector and calling endInsertRows() afterwards -- exactly the same that I do in addKurs(), as far as I understand. So, I thought I did not need an extra function doing that but can also do the same three lines in TreeModel::addKurs().
What is the difference between calling insertRows() and writing the code in addKurs()?
-
@Pl45m4 said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):
If addKurs() works as your insertRows() implementation, the line between begin and end is invalid or may cause issues.
I'm wondering why you use this begin and end functions, when you dont implement insertRows().Then I do not quite understand the concept of Qt. I looked at some examples, and insertRows() usually does nothing besides calling beginInsertRows(), appending something to a QVector and calling endInsertRows() afterwards -- exactly the same that I do in addKurs(), as far as I understand. So, I thought I did not need an extra function doing that but can also do the same three lines in TreeModel::addKurs().
What is the difference between calling insertRows() and writing the code in addKurs()?
@Stephan-Lorenzen said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):
What is the difference between calling insertRows() and writing the code in addKurs()?
Of course you can write your own "API" to handle your model / data.
But you should really debug your code to check your "live" values and to see what's going on. It's hard to say how your data / model and indices really behave while inserting.If you use QtCreator you can set a breakpoint by clicking next to the number of your line of code. A red dot appears and the debugger stops there, so you can check your vars and result.
k->semester()->addKurs(k)This looks like some sort of parent/child "circle".
Semesteris a part ofKursbut then you add the same Kurs k to yourSemester. Probablykgets re-parented, depending on whatSemester::addKurs()does. -
@Stephan-Lorenzen said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):
What is the difference between calling insertRows() and writing the code in addKurs()?
Of course you can write your own "API" to handle your model / data.
But you should really debug your code to check your "live" values and to see what's going on. It's hard to say how your data / model and indices really behave while inserting.If you use QtCreator you can set a breakpoint by clicking next to the number of your line of code. A red dot appears and the debugger stops there, so you can check your vars and result.
k->semester()->addKurs(k)This looks like some sort of parent/child "circle".
Semesteris a part ofKursbut then you add the same Kurs k to yourSemester. Probablykgets re-parented, depending on whatSemester::addKurs()does.@Pl45m4 said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):
This looks like some sort of "circle". Semester is a part of Kurs but then you add the same Kurs k to your Semester.
That's also my idea - therefore the hint with the debugger to see the stack trace.
-
No, Semester is the parent of Kurs. Semester has a vector of Kurs*, and each Kurs has a poinzter to its parent, a Semester.