[英]Qt: c++: How to create a SIGNAL/SLOT when selecting a row in QTableView
我有一个QTableView
正常工作在GUI上显示我的模型。 但是,我想创建一个“SIGNAL / SLOT”,当我从QTableView
选择一行时,它会起作用。
我怎样才能做到这一点?
你可以这样做:
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
}
我希望这可以帮到你。
使用选择模型( docs )中的currentRowChanged(const QModelIndex & current, const QModelIndex & previous)
信号。
请参阅文档QAbstractItemView https://qt-project.org/doc/qt-4.7/qabstractitemview.html
void QAbstractItemView已激活 (const QModelIndex&index)[signal]
当用户激活index指定的项目时,将发出此信号。 如何激活项目取决于平台; 例如,通过单击或双击项目,或在项目为当前时按Return或Enter键。
并使用QModelIndex :: row()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.