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

3

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?

Psychopath answered 23/4, 2013 at 14:13 Comment(0)
R
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.

Rosebay answered 2/9, 2015 at 23:38 Comment(0)
S
2

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

Sheree answered 23/4, 2013 at 14:16 Comment(0)
I
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()

Infusion answered 23/4, 2013 at 14:42 Comment(1)
Activation is not the same as selection.Sheree

© 2022 - 2024 — McMap. All rights reserved.