PDA

View Full Version : Display data from QStandardItemModel in QTreeView



jprice01801
9th January 2007, 15:36
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;


QStandardItemModel * model = new QStandardItemModel;
QStandardItemItem * parentItem = model->invisibleRootItem();

I iterate over my data instantiating new child items to be added to the model.

QStandardItem * item = new QStandardItem();
item->setData(QVariant("some data"));
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;


treeView->setModel(model);
QStandardItem * parentItem = model->invisibleRootItem();
QModelIndex index = parentItem->index();
treeView->setRootIndex(index);
QModelIndex currentIndex = model->index(0,0);
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

wysota
9th January 2007, 15:49
My guess is you don't have any rows in your model - try using QStandardItemModel::appendRow

jprice01801
9th January 2007, 17:39
My guess is you don't have any rows in your model - try using QStandardItemModel::appendRow

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.

wysota
9th January 2007, 20:32
Try this:

QStandardItemModel *model = new QStandardItemModel(this);
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");
QTreeView *treeView = new QTreeView(this);
treeView->setModel(model);

jprice01801
9th January 2007, 20:35
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.

wysota
9th January 2007, 20:43
void QStandardItem::setData ( const QVariant & value, int role = Qt::UserRole + 1 )

setText() is equivalent to
setData("some data", Qt::DisplayRole);

jprice01801
10th January 2007, 00:41
setText() is equivalent to
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.

wysota
10th January 2007, 09:34
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.