Display data from QStandardItemModel in QTreeView
Hi, this is my first post to the forum. I have an application that queries for data on a network. Then populates a QStandardItemModel in the following fashion;
Code:
QStandardItemItem * parentItem = model->invisibleRootItem();
I iterate over my data instantiating new child items to be added to the model.
Code:
parentItem->setChild(row,column,item);
Now the data is in the model, I have gone through it and written the data back out to the console.
Next I set the model in my treeview in this way;
Code:
treeView->setModel(model);
treeView->setRootIndex(index);
treeVeiw->setCurrentIndex(currentIndex);
treeView->setRootIndex(index);
When the application runs and I get to the page with the treeview display, I see the model column header labels but no data.
I don't understand why I don't see the data, any assistance would be appreciated.
thanks
Re: Display data from QStandardItemModel in QTreeView
My guess is you don't have any rows in your model - try using QStandardItemModel::appendRow
Re: Display data from QStandardItemModel in QTreeView
Quote:
Originally Posted by
wysota
I was passing the row and column arguments in the parentItem->setChild(row,column,item) call.
I tried your suggestion in two ways first just adding the extra call, still no data and a warning from qt about duplicate items. The display showed two faint angle lines on the left margin that should be followed by the data.
The second try with just the appendRow() call yeilded the same are results of the original implementation, no data and one faint angle line on the left margin.
Re: Display data from QStandardItemModel in QTreeView
Try this:
Code:
model->setColumnCount(1);
model->setRowCount(5);
model->setData(model->index(0, 0), "some data0");
model->setData(model->index(1, 0), "some data1");
model->setData(model->index(2, 0), "some data2");
model->setData(model->index(3, 0), "some data3");
model->setData(model->index(4, 0), "some data4");
treeView->setModel(model);
Re: Display data from QStandardItemModel in QTreeView
I found the resolution to displaying data in my QTreeView object. What I found was that not only do you have to set the item data but you must also set the item text. In my initialization of the model after setting the item data I added the following line and now see all data in my model displayed in the treeview.
item->setText(tr("some data"));
My guess is that it is done this way for internationalization.
Re: Display data from QStandardItemModel in QTreeView
Quote:
void QStandardItem::setData ( const QVariant & value, int role = Qt::UserRole + 1 )
setText() is equivalent to
Code:
setData("some data", Qt::DisplayRole);
Re: Display data from QStandardItemModel in QTreeView
Quote:
Originally Posted by
wysota
setText() is equivalent to
Code:
setData("some data", Qt::DisplayRole);
Now thats interesting. When I populated the model I was aware of the "roles" but obviously didn't understand the implications. Thanks for the information.
Re: Display data from QStandardItemModel in QTreeView
QStandardItem (and other convenience item classes like QTreeWidgetItem) simply has convenience methods like setText, setToolTip, setFont, setIcon, etc. that are wrappers to setData with appropriate roles.