zoukankan      html  css  js  c++  java
  • 如何检测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);
  • 相关阅读:
    刷题86—动态规划(三)
    刷题85—动态规划(二)— 股票6道
    刷题84—动态规划(一)
    刷题83——硬币
    刷题82——二叉树的右视图
    刷题81——统计「优美子数组」
    android adb 流程原理代码分析(一)
    android默认开启adb调试方法分析
    recovery 下界面UI旋转90 180 270修改
    sublime使用Package Control不能正常使用的解决办法
  • 原文地址:https://www.cnblogs.com/justwake/p/justwake_qitemselectionmodel.html
Copyright © 2011-2022 走看看