Qt:c ++:如何在QTableView中选择行时创建SIGNAL/SLOT

McL*_*Lan 7 c++ qt model qtableview

我有一个QTableView正常工作在GUI上显示我的模型.但是,我想创建一个"SIGNAL/SLOT",当我从中选择一行时,它可以工作QTableView.

我怎样才能做到这一点?

Ang*_*ano 5

您可以通过以下方式进行操作:

connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
             SLOT(slotSelectionChange(const QItemSelection &, const QItemSelection &))
            );
Run Code Online (Sandbox Code Playgroud)

插槽将是:

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
}
Run Code Online (Sandbox Code Playgroud)

希望对您有所帮助。


cma*_*t85 2

使用currentRowChanged(const QModelIndex & current, const QModelIndex & previous)选择模型中的信号(文档)。