McLan
McLan

Reputation: 2688

Qt: c++: How to create a SIGNAL/SLOT when selecting a row in QTableView

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?

Upvotes: 8

Views: 6236

Answers (3)

Angie Quijano
Angie Quijano

Reputation: 4473

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.

Upvotes: 6

Mao Shultz
Mao Shultz

Reputation: 86

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()

Upvotes: 2

cmannett85
cmannett85

Reputation: 22376

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

Upvotes: 2

Related Questions