[QT4 & XP] retrieve data from QTreeView's model
hi,:o
in the following code table is a QTreeView based on a QStandardItemModel with 2 columns:
Code:
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 :
Code:
void MainWindow::selection(const QModelIndex& idx)
{
}
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 ?
Re: [QT4 & XP] retrieve data from QTreeView's model
You can "iterate" in your model by using QModelIndex methods parent(), child(), sibling()..
One way to fetch data in both colums:
Code:
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:
Code:
QString col0
= idx.
sibling(idx.
row(),
0).
data().
toString();