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

8

我有一个QTableView,它能够正常地在GUI上显示我的模型。但是,我想创建一个“SIGNAL/SLOT”,当我从QTableView中选择一行时,它能够正常工作。

我该如何做到这一点?

3个回答

6
你可以这样做:
connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
             SLOT(slotSelectionChange(const QItemSelection &, const QItemSelection &))
            );

而插槽将会是:

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
}

我希望这能帮助到你。


2

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


2
请参见 QAbstractItemView 文档 https://qt-project.org/doc/qt-4.7/qabstractitemview.html
当用户激活索引所指定的项目时,将触发此信号。如何激活项目取决于平台; 例如,通过单击或双击项目,或在项目为当前项时按下“Return”或“Enter”键等。
并使用 QModelIndex::row() 函数。

1
激活与选择不是同一回事。 - cmannett85

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接