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

Determine single selected row in a table view

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.6k 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on 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
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on 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);
        
        JonBJ VRoninV 2 Replies Last reply
        0
        • PerdrixP Perdrix

          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);
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          • PerdrixP Perdrix

            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);
            
            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on 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

            • Login

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