8

I have a QTableView which is working properly showing my model on the GUI. however, I would like to create a "SIGNAL/SLOT" that works when I select a row from the QTableView.

How can I do that?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
McLan
  • 2,552
  • 9
  • 51
  • 85

3 Answers3

6

You can do it in this way:

connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
             SLOT(slotSelectionChange(const QItemSelection &, const QItemSelection &))
            );

And the slot would be:

void MainWindow::slotSelectionChange(const QItemSelection &, const QItemSelection &)
{
            QModelIndexList selection = ui->tableView->selectionModel()->selectedRows();//Here you are getting the indexes of the selected rows

            //Now you can create your code using this information
}

I hope this can help you.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
2

Use the currentRowChanged(const QModelIndex & current, const QModelIndex & previous) signal from the selection model (docs).

cmannett85
  • 21,725
  • 8
  • 76
  • 119
2

See the documentation QAbstractItemView https://qt-project.org/doc/qt-4.7/qabstractitemview.html

void QAbstractItemView activated (const QModelIndex &index ) [signal]

This signal is emitted when the item specified by index is activated by the user. How to activate items depends on the platform; e.g., by single- or double-clicking the item, or by pressing the Return or Enter key when the item is current.

And use QModelIndex::row()

Community
  • 1
  • 1
Mao Shultz
  • 76
  • 3