PDA

View Full Version : qtreeview item's data



Pawello
23rd August 2012, 09:36
Hi,
I have a problem with QTreeView. I want to read QString from selected cell, or (starting) index of this cell but I dont know how to do it. I've tried to use currentIndex(), but it shows position in current View- no matter which item is on first place in window after sorting or filtering it's index is always 0,0.

spirit
23rd August 2012, 11:50
Did you use QAbstractItemModel::data?

Pawello
23rd August 2012, 13:03
this could help, but I dont know how can I use it to extract int value from e.g. 5. cell of selected row.

spirit
23rd August 2012, 13:12
Using QVariant::toInt.

Pawello
23rd August 2012, 13:33
I've found this, but how to get data from e.g 5th. cell of the row.
E.g. No matter if I choose item at x,0 or x,1 I want to read data from x,5



And one more question: how can I make one header column invisible?

spirit
23rd August 2012, 13:37
I've found this, but how to get data from e.g 5th. cell of the row.
E.g. No matter if I choose item at x,0 or x,1 I want to read data from x,5


Use QAbstractItemModel::index to get necessary index. Then you can use it for getting data:


...
qDebug() << Q_FUNC_INFO << model->data(model->index(0, 5));
...




And one more question: how can I make one header column invisible?

See QHeaderView::hideSection.

Pawello
23rd August 2012, 14:45
Use QAbstractItemModel::index to get necessary index. Then you can use it for getting data:


...
qDebug() << Q_FUNC_INFO << model->data(model->index(0, 5));
...



I dont'understand what do you mean. I suppose that


data(model->index(0, 5))

will always give me data from index 0,5
My problem is that I want to use current index' row and set index column as 5- only column must be constant, row depends on the chosen cell.





See QHeaderView::hideSection.

Thanks, I used QTreeView::setColumnHidden

spirit
23rd August 2012, 14:50
Did you try this?


...
QVariant value = model->data(model->index(view->currentIndex().row(), 5));
...

Pawello
23rd August 2012, 15:22
It still doesn't work.
I have:


QTreeView tab;
QStandardItemModel model;
...
int s=tab->currentIndex().row();
int a=model->data(model->index(s,5)).toInt();
...


Working code, which needs selecting items in 5th column :


QTreeView tab;
QStandardItemModel model;
...
int a=tab->currentIndex().data().toInt();
...

spirit
23rd August 2012, 15:24
That's because the current index == (0, 0) or invalid.

Pawello
23rd August 2012, 15:33
So what should I do? I don't see any difference between my code and code posted by you previously, but it doesn't work properly.

yeye_olive
23rd August 2012, 15:46
What does "it doesn't work" mean? At this point no one can do anything for you beyond playing guessing games.

By the way, row and column numbering is 0-based, so if you are interested in the 5th column you will should use model->data(model->index(view->currentIndex().row(),4)).

Pawello
23rd August 2012, 15:55
I know about starting numeration from 0.
When I'm using second code program works well and read pointed cell's info, no matter if view was sorted, filtered... but when I use first code it shows number depending on position of item in current view, not information from 5th column. It use current index of item instead of value from 5th column.

spirit
23rd August 2012, 16:06
Try to debug your app. Or provide complete compilable code.