Getting current index from a QListView model
i am inserting items to a QListView using a QStandardItem model.
when i am selecting an item from the listview, i want to retrieve the index in a sequential manner.
instead of going 0,1,2... and so on i get values 1,3,5,7(the row values)
so how can i get a sequential value from the list view i.e. first element selected gives 1 and second gives 2 and so on...
is there any method to get current index of selected item ?? i am fetching the current row which comes like 1,3,5 etc
You can set user data for item. Look at "doc":https://qt-project.org/doc/qt-4.8/qstandarditem.html#setData . If i am not mistake you can set several data to one item, like:
@item - > setData(const QVariant & value, Qt::UserRole + 1 );
item - > setData(const QVariant & value, Qt::UserRole + 2 );
item - > setData(const QVariant & value, Qt::UserRole + 3 );
correct me if i'm wrong.
Hi subrat17june ,
You can connect the clicked(QModelIndex) signal of QListView with any slot which receive the QModelIndex return from QListView. Using that QModelIndex, you can call the row() function of QModelIndex class which gives you row value.
QTreeView *treeView = new QTreeView(this);
When you receive the signal, you call row() on the given model index to get a row value of the item:
void MyWidget::clicked(const QModelIndex &index)
int row = index.row();
//Do your stuff with row value.
I am getting the row...
but the value is the issue i am having..
values are not contiguous
i want values to be either 0,1,2,3 and so on or 1,2,3,...and so on
but the values i am getting are 1 ,3 5, 7 ,9
is there a way to get values serially of the current index selected?
What you need is to look into [[doc:QItemSelectionModel]]. Every item view exposes one. It gives you much more control over the item selection, and makes it quite easy to iterate over the selected items.