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. Selecting a row in QTreeView programmatically

Selecting a row in QTreeView programmatically

Scheduled Pinned Locked Moved Unsolved General and Desktop
login
5 Posts 2 Posters 2.7k Views
  • 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.
  • N Offline
    N Offline
    NoumanYosuf
    wrote on last edited by
    #1

    Hi all,

    I have been trying to select a row (also go to that row) from a qtreeview problematically but could not achieve it.
    I know there are already couple of questions for the same but none of them could help me to get what I expect.

    I have tried below codes :

    QModelIndex idx= m_pOutlineTree->model()->index(3, 0, index_view);
    //index_view is my root QModelIndex of complete qtreeview and 3,0 is the row no I have to go to.
    
    /*1.*/ myTreeView->setCurrentIndex(idx);
    
    /*2.*/myTreeView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    

    But none of the above works as intended.

    Could anyone please guide me what I am missing or a better way to achieve this.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Did you check if idx is acutally valid?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • N Offline
        N Offline
        NoumanYosuf
        wrote on last edited by
        #3

        Thank you for the reply.
        Yes, Its valid. I tried to print the data, row and column to check if it actually the one i intent it to be. And its fine.

        QVariant data = m_pOutlineTree->model()->data(idx);
                QString text = data.toString();
                qDebug()<<text + "  " + QString::number(idx.row()) + "  " + QString::number(idx.column());
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @NoumanYosuf said in Selecting a row in QTreeView programmatically:

          index_view is my root QModelIndex

          I'm asking because this somehow makes no sense - when it's the root index then index_view is a simple, invalid QModelIndex.
          Please check that QTreeView::currentIndex() actually returns your index.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • N Offline
            N Offline
            NoumanYosuf
            wrote on last edited by
            #5

            currentIndex() returns a valid index.I am not sure where is the mistake.

            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