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

I have QTableViewone that works correctly by showing my model in a graphical interface. however, I would like to create a "SIGNAL / SLOT" that works when I select a line from QTableView.

How can i do this?

+5
source share
3 answers

You can do it as follows:

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

And the slot will 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
}

Hope this helps you.

+3
source

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

+2
source

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

void QAbstractItemView (const QModelIndex & index) [signal]

This signal is emitted when the element indicated by the index is activated by the user. How to activate the elements depends on the platform; for example, by single or double clicking on an item, or by pressing the "Return" or "Enter key, when the item is current.

And use QModelIndex :: row ()

+2
source

All Articles