如何检测QTableView中是否有选择项
如何检测QTableView中是否有选择项
使用QItemSelectionModel类
QItemSelectionModel类说明: The QItemSelectionModel class keeps track of a view's selected items. A QItemSelectionModel keeps track of the selected items in a view, or in several views onto the same model. It also keeps track of the currently selected item in a view. The QItemSelectionModel class is one of the Model/View Classes and is part of Qt's model/view framework. QItemSelectionModel类保持与视图选择项之间的联系! 一个QItemSelectionModel保持着与一个视图(或者同一个model的多个视图)的选择项之间的联系。 QItemSelectionModel类属于模型-视图类,并且它属于Qt的模型-视图框架的一部分。
操作:触发QItemSelectionModel的selectionChanged信号,然后在响应槽中通过该类的hasSelection方法判断是否有选择项目。
可以通过currentIndex获取当前选择项。
eg. QItemSelectionModel * md = ui->tableView->selectionModel(); connect(md,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(view_select_check (QItemSelection,QItemSelection))); 然后在响应槽函数当中: if(wordTableSelectionModel->hasSelection()) ui->selectOk->setEnabled(true); else ui->selectOk->setDisabled(true); 至于获取当前选择项,可以使用currentIndex获取当前选择项的QModelIndex,然后进行处理,我这里是投递到它的双击事件中。 QModelIndex dex = wordTableSelectionModel->currentIndex(); this->on_tableView_doubleClicked(dex);