View Full Version : [QT4 & XP] retrieve data from QTreeView's model

2nd March 2006, 12:39

in the following code table is a QTreeView based on a QStandardItemModel with 2 columns:

connect( table, SIGNAL( doubleClicked(const QModelIndex&) ),
this, SLOT( selection(const QModelIndex&) ) );

When the slot is activated, I would like to retrieve the selected data from the model like :

void MainWindow::selection(const QModelIndex& idx)
QString col0 = model->data(QModelIndex(idx)).toString();

This returns the selected item allright (col0 or col1 depending on the user's selection).
But I need to retrieve data for both columns.
How is that achieved ?

2nd March 2006, 13:02
You can "iterate" in your model by using QModelIndex methods parent(), child(), sibling()..
One way to fetch data in both colums:

QString col0 = model->data(idx.sibling(idx.row(), 0)).toString();
QString col1 = model->data(idx.sibling(idx.row(), 1)).toString();

Edit: Btw, you don't need to store a pointer to the model as a member variable for this kind of purpose.
You can reach the data by using only model indexes as well:

QString col0 = idx.sibling(idx.row(), 0).data().toString();