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. Determine single selected row in a table view
Forum Updated to NodeBB v4.3 + New Features

Determine single selected row in a table view

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.6k 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.
  • P Offline
    P Offline
    Perdrix
    wrote on 12 Jun 2021, 15:21 last edited by
    #1

    I have a table view that's set for Extended Selection. Is the following correct code to determine if a single row is selected, and if so which row number it is? If correct is this the "best way" or is there a better way to do it?

    //Assume that the following has been done:
    //	connect(ui->tableView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(tableViewItemClickedEvent(const QModelIndex&)));
    
    void StackingDlg::tableViewItemClickedEvent(const QModelIndex& index)
    {
    	qDebug() << "Table View item clicked, row " << index.row();
    	QItemSelectionModel * qsm = ui->tableView->selectionModel();
    	QModelIndexList selectedRows = qsm->selectedRows();
    	//
    	// If only one row is selected, we want to know the filename
    	//
    	if (1 == selectedRows.count())
    	{
    		QModelIndex& index = selectedRows[0];
    		if (index.isValid())
    		{
    			const DSS::ImageListModel* model = dynamic_cast<const DSS::ImageListModel*>(index.model());
    			int row = index.row();
    			m_strShowFile = model->selectedFileName(row);
    		}
    	}
    }
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 12 Jun 2021, 16:31 last edited by
      #2

      Very close. QModelIndexList selectedRows = qsm->selectedRows(); should be QModelIndexList selectedRows = qsm->selectedIndexes();

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Perdrix
        wrote on 12 Jun 2021, 17:26 last edited by
        #3

        Please could you explain why QItemSelectionModel::selectedRows() isn't correct? Or put another way - what's the crucial difference?

        PS the code I have to set up the table view reads:

                tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
                tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
        
        J V 2 Replies Last reply 12 Jun 2021, 17:47
        0
        • P Perdrix
          12 Jun 2021, 17:26

          Please could you explain why QItemSelectionModel::selectedRows() isn't correct? Or put another way - what's the crucial difference?

          PS the code I have to set up the table view reads:

                  tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
                  tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
          
          J Offline
          J Offline
          JonB
          wrote on 12 Jun 2021, 17:47 last edited by
          #4

          @Perdrix said in Determine single selected row in a table view:

          PS the code I have to set up the table view reads:
          tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

          Given this I think your use of selectedRows() is correct. Maybe not knowing that affected @VRonin's answer.

          1 Reply Last reply
          1
          • P Perdrix
            12 Jun 2021, 17:26

            Please could you explain why QItemSelectionModel::selectedRows() isn't correct? Or put another way - what's the crucial difference?

            PS the code I have to set up the table view reads:

                    tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
                    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
            
            V Offline
            V Offline
            VRonin
            wrote on 12 Jun 2021, 17:47 last edited by
            #5

            @Perdrix said in Determine single selected row in a table view:

            tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

            In this case they are equivalent.

            The doc explains the difference pretty well:

            QModelIndexList QItemSelectionModel::selectedRows(int column = 0) const
            Returns the indexes in the given column for the rows where all columns are selected.

            So if you selected a single cell in a row selectedRows would not return it

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2

            3/5

            12 Jun 2021, 17:26

            • Login

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