Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting current index from a QListView model
Forum Updated to NodeBB v4.3 + New Features

Getting current index from a QListView model

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 13.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    subrat17june
    wrote on last edited by
    #1

    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

    Thank You.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi subrat17june!
      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 );
      etc...@
      correct me if i'm wrong.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Himanshu Rohilla
        wrote on last edited by
        #3

        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.

        For example:-

        @
        QTreeView *treeView = new QTreeView(this);
        treeView->setModel(myStandardItemModel);
        connect(treeView, SIGNAL(clicked(QModelIndex)),
        this, SLOT(clicked(QModelIndex)));
        @

        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. 
        

        }
        @

        HImanshu Rohilla

        1 Reply Last reply
        0
        • S Offline
          S Offline
          subrat17june
          wrote on last edited by
          #4

          Hi,
          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?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved