Solved QListStringModel setData not set data
-
Hey, I'm making an education project text editor
I list opened file in a dock (QListView). I need to handle clicks on rows in listview, so I store indexes of QSLM in a QMap modelListLinks. In first inserting row in QSLM everything is fine, but then setData just not works and title of files in dock is empty.
Indexes are correct, because when I click on rows they change active tab properly.
Function that creates rowsvoid MainWindow::on_actionNew_triggered() { QTextEdit* textEdit = new QTextEdit(tabs); textEdits.insert(textEdit, ""); tabs->addTab(textEdit, "Безымянный"); modelList->insertRow(0); QModelIndex index = modelList->index(modelList->rowCount()-1); modelList->setData(index, "Безымянный", Qt::DisplayRole); modelListLinks.insert(index, textEdit); modelList->setData(index, fileInfo.fileName(), Qt::DisplayPropertyRole); } ``` The part of constructor, which declares model
tabs->addTab(textEdit, fileInfo.fileName()); modelList->insertRow(0); QModelIndex index = modelList->index(modelList->rowCount()-1); modelList->setData(index, fileInfo.fileName(), Qt::DisplayPropertyRole); modelListLinks.insert(index, textEdit);

-
Hi and welcome to the forums.
is this a custom model ?
You might need to call
http://doc.qt.io/qt-5/qabstractitemmodel.html#beginInsertRowsAh sorry its a QListStringModel ( non subclass, i assume ?)
if we look at this sample
http://www.bogotobogo.com/Qt/Qt5_QListView_QStringListModel_ModelView_MVC.php
Im wondering if (from your sample)
modelList->insertRow(0);
QModelIndex index = modelList->index(modelList->rowCount()-1);
Get the wrong index as
http://doc.qt.io/qt-5/qabstractitemmodel.html#insertRow
says it inserts before the given parent ( which is empty QModelIndex() )
but you take out index for last row.
When you click button, does it insert teh "empty" ones in bottom ? or top ? -
@mrjj said in QListStringModel setData not set data:
is this a custom model ?
it's not a subclass, pure QListStringModel
Here are declarations of used
QMap<QModelIndex, QWidget*> modelListLinks;
QStringListModel* modelList; -
@asiaron
Ok plain standard.
I update the answer. Im not sure you get the right index of the just inserted. -
Yes, I not properly read documentation and really mess with indexes, thank you for the answer
.