QTreeView empty
-
Tried to implement a custom model from
QAbstractItemModel
but whenever I try to display the data in a QTreeView it's just empty.
QAbstractItemModelTester doesn't seems to detect anything and manually getting data like thismodel->data(model->index(row, column), 0).toString()
works without any problems as well.Model implementation: https://pastebin.com/w9TWaTCb
TreeItem (Components) implementation: https://pastebin.com/vqjYThzJ -
And how/where do you use them?
-
This code is being invoked after a button press:
JsonProcessor processor("C:/Users/admin/source/qt-repos/Jser/Inputs/1.json"); JsonModel *model = processor.getModel(); QAbstractItemModelTester *tester = new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal); ui->treeView->setModel(model);
The JsonProcessor class creates the TreeView items based on a json file
JsonProcessor implementation: https://pastebin.com/fThF73PF -
Hi
as far as I remember, QAIMT only performs type validation checks. this means, for a particular role, it verifies that the data returned for that role is convertible to the datatype that the default delegates expects.
In other words, it just contents itself verifying that data for display role can be converted to QString, that background role can be converted to QBrush, etc etc ... As far as I remember, an empty QVariant passes the test.
But it's been a while I haven't used Qt's predefined roles so I might be wrong.What you should try is display to console the first cell for root item, and repeat the process for a few cells, first in root item, then get first cell of first child :
QModelIndex firstCell = model->index(0, 0, QModelIndex()) ; qDebug() << data (firstCell, Qt::DisplayRole) ;
So of course Qt::DisplayRole is not needed as it's already the default value.
The empty QModelIndex isn't either for accessing values in root item, but you will need of course to specify a parent once you try to access a cell within a child node.This for sure will tell you if you are actually getting the value that you are expecting.
I have never worked with trees having multiple columns, I haven't digged much into it, but this seems to me a little suspicious :
int JsonModel::rowCount(const QModelIndex &parent) const { // Only column 0 has children if (parent.column() > 0) return 0;
at multiple times, you get the parent of the currentItem, to get the child of the parent next. That seems to me a little weird also.
-
This code is being invoked after a button press:
JsonProcessor processor("C:/Users/admin/source/qt-repos/Jser/Inputs/1.json"); JsonModel *model = processor.getModel(); QAbstractItemModelTester *tester = new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal); ui->treeView->setModel(model);
The JsonProcessor class creates the TreeView items based on a json file
JsonProcessor implementation: https://pastebin.com/fThF73PF@MisterOrange said in QTreeView empty:
JsonProcessor processor("C:/Users/admin/source/qt-repos/Jser/Inputs/1.json");
JsonModel *model = processor.getModel();
Basic c++ - how long does the JsonProcessor instance (and therefore the model) live?
-
@Christian-Ehrlicher yep that was it. Can't believe I missed that for so long
thank you both for the help
-