QTreeView does not show data
-
Dear Forum,
I am trying to redo the QTreeView minimal example described in
https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.htmlBasically, all my code is from that side. However, the TreeView is empty and shows no data at all:

What am i doing wrong?
MainWindow::MainWindow() : QMainWindow(), treeView(new QTreeView(this)), treeModel(nullptr) { setCentralWidget(treeView); // initialize items with parent and row number TreeItem *rootItem = new TreeItem(nullptr, -1); TreeItem *item_0 = new TreeItem(rootItem, 0); TreeItem *item_1 = new TreeItem(rootItem, 1); TreeItem *item_0_0 = new TreeItem(item_0, 0); TreeItem *item_0_1 = new TreeItem(item_0, 1); // add children to parenta rootItem->child.push_back(item_0); rootItem->child.push_back(item_1); item_0 ->child.push_back(item_0_0); item_0 ->child.push_back(item_0_1); treeModel = new TreeModel(rootItem); treeView->setModel(treeModel); }QModelIndex TreeModel::index(int rowNr, int column, const QModelIndex& parent) const { if (!hasIndex(rowNr, column, parent)) return QModelIndex(); const TreeItem *parentTI; if (!parent.isValid()) parentTI = rootItem; else parentTI = static_cast<TreeItem*>(parent.internalPointer()); TreeItem *ti = parentTI->child[rowNr]; return (createIndex(rowNr, column, ti)); } QModelIndex TreeModel::parent(const QModelIndex& child) const { if (!child.isValid()) return QModelIndex(); TreeItem *childItem = static_cast<TreeItem*>(child.internalPointer()); TreeItem *parentItem = childItem->parent; if (parentItem == rootItem) return QModelIndex(); return(createIndex(parentItem->row, 0, parentItem)); } int TreeModel::rowCount(const QModelIndex &parent) const { const TreeItem *parentTI; if (!parent.isValid()) parentTI = rootItem; else parentTI = static_cast<TreeItem*>(parent.internalPointer()); return (parentTI->child.size()); } int TreeModel::columnCount(const QModelIndex &) const {return (2);} QVariant TreeModel::data(const QModelIndex &index, int) const { return(QVariant(QString::fromStdString("TEST"))); }Best
Stephan
-
Dear Forum,
I am trying to redo the QTreeView minimal example described in
https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.htmlBasically, all my code is from that side. However, the TreeView is empty and shows no data at all:

What am i doing wrong?
MainWindow::MainWindow() : QMainWindow(), treeView(new QTreeView(this)), treeModel(nullptr) { setCentralWidget(treeView); // initialize items with parent and row number TreeItem *rootItem = new TreeItem(nullptr, -1); TreeItem *item_0 = new TreeItem(rootItem, 0); TreeItem *item_1 = new TreeItem(rootItem, 1); TreeItem *item_0_0 = new TreeItem(item_0, 0); TreeItem *item_0_1 = new TreeItem(item_0, 1); // add children to parenta rootItem->child.push_back(item_0); rootItem->child.push_back(item_1); item_0 ->child.push_back(item_0_0); item_0 ->child.push_back(item_0_1); treeModel = new TreeModel(rootItem); treeView->setModel(treeModel); }QModelIndex TreeModel::index(int rowNr, int column, const QModelIndex& parent) const { if (!hasIndex(rowNr, column, parent)) return QModelIndex(); const TreeItem *parentTI; if (!parent.isValid()) parentTI = rootItem; else parentTI = static_cast<TreeItem*>(parent.internalPointer()); TreeItem *ti = parentTI->child[rowNr]; return (createIndex(rowNr, column, ti)); } QModelIndex TreeModel::parent(const QModelIndex& child) const { if (!child.isValid()) return QModelIndex(); TreeItem *childItem = static_cast<TreeItem*>(child.internalPointer()); TreeItem *parentItem = childItem->parent; if (parentItem == rootItem) return QModelIndex(); return(createIndex(parentItem->row, 0, parentItem)); } int TreeModel::rowCount(const QModelIndex &parent) const { const TreeItem *parentTI; if (!parent.isValid()) parentTI = rootItem; else parentTI = static_cast<TreeItem*>(parent.internalPointer()); return (parentTI->child.size()); } int TreeModel::columnCount(const QModelIndex &) const {return (2);} QVariant TreeModel::data(const QModelIndex &index, int) const { return(QVariant(QString::fromStdString("TEST"))); }Best
Stephan
@Stephan-Lorenzen said in QTreeView does not show data:
QVariant TreeModel::data(const QModelIndex &index, int) const
{
return(QVariant(QString::fromStdString("TEST")));
}This is wrong - you should only return something for Qt::DisplayRole.
btw: Why convert a plain char string to a std::string just to convert it to a QString? QString can take a plain char* too.
-
Wow, you are great -- it works!! Thanks a lot!
Just for curiosity - what exactly goes wrong if data() returns a string for another role? I see things like TooltipRole or StatustipRole, it seems to me that it should not harm if something would be returned for these roles? -
Wow, you are great -- it works!! Thanks a lot!
Just for curiosity - what exactly goes wrong if data() returns a string for another role? I see things like TooltipRole or StatustipRole, it seems to me that it should not harm if something would be returned for these roles?@Stephan-Lorenzen said in QTreeView does not show data:
it seems to me that it should not harm if something would be returned for these roles?
There are also roles like e.g. the font role or color roles. I would guess it's the font role.
-
@Stephan-Lorenzen said in QTreeView does not show data:
it seems to me that it should not harm if something would be returned for these roles?
There are also roles like e.g. the font role or color roles. I would guess it's the font role.
@Christian-Ehrlicher
I sort of guessed it might be that too, becauseQFontcontructor takes a string as a "family name". But it says/implies it will pick some other font when it doesn't recognise "TEST"?