Qt MVC结构之QItemSelectionModel模型介绍

QItemSelectionModel

Qt的MVC结构支持多个View共享同一个model,包括该model的选中状态等。我们可以通过设置QItemSelectionModel,来更改View的选中效果和显示效果。我们创建一个Qt Application项目,在MainWindow的头文件中添加一个QTbaleView*类型的成员 _table_view。然后在构造函数中为这个_table_view设置model

  1. MainWindow::MainWindow(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainWindow)
  4. {
  5. ui->setupUi(this);
  6. QStandardItemModel * model = new QStandardItemModel(7,4,this);
  7. for(int row=0; row < 7; row++){
  8. for(int column = 0; column < 4; column++){
  9. QStandardItem * item = new QStandardItem(QString("%1").arg(row*4+column));
  10. model->setItem(row, column, item);
  11. }
  12. }
  13. _table_view = new QTableView;
  14. _table_view->setModel(model);
  15. setCentralWidget(_table_view);
  16. this->resize(800,800);
  17. }

我们创建了一个QStandardItemModel对象,然后为其设置几个Item,最后将这个model设置到tableview中。
我们可以为TableView设置选择的项目,接下来继续在构造函数中补充

  1. //获取视图的项目选择模型
  2. QItemSelectionModel * selection_model = _table_view->selectionModel();
  3. //定义左上角和右下角的索引
  4. QModelIndex topLeft;
  5. QModelIndex bottomRight;
  6. //根据上面两个索引选择项目
  7. //第1行1列
  8. topLeft = model->index(1,1,QModelIndex());
  9. //第5行2列
  10. bottomRight = model->index(5,2,QModelIndex());
  11. //设置选择区域
  12. QItemSelection selection(topLeft, bottomRight);
  13. //将选择的区域设置给选择模型, 设置 为选中状态
  14. selection_model->select(selection, QItemSelectionModel::Select);

我们从tableview中获取选择模型,然后从根节点下选择第1行1列作为左上索引,选择第5行2列作为右下索引。然后创建选择区域QItemSelection,最后将选择区域这只给选择模型。接下来为了我们为工具栏添加两个动作,为动作绑定两个槽函数,在MainWindow声明文件中添加两个函数的声明

  1. public slots:
  2. void getCurrentItemData();
  3. void toggleSelection();

在构造函数中将动作和槽函数绑定起来

  1. ui->mainToolBar->addAction(tr("当前项目"), this, &MainWindow::getCurrentItemData);
  2. ui->mainToolBar->addAction(tr("切换选择"), this, &MainWindow::toggleSelection);

实现这两个槽函数, getCurrentItemData获取当前条目的数据信息,toggleSelection实现切换选择的条目

  1. void MainWindow::getCurrentItemData(){
  2. auto currentData = _table_view->selectionModel()->currentIndex().data().toString();
  3. qDebug() <<tr("当前项目的内容") << currentData;
  4. }
  5. void MainWindow::toggleSelection(){
  6. //找到根节点下第0行0列的item的索引
  7. QModelIndex topLeft = _table_view->model()->index(0,0,QModelIndex());
  8. //获取根节点下最大的行号
  9. auto max_row = _table_view->model()->rowCount(QModelIndex());
  10. //获取根节点下最大的列号
  11. auto max_column = _table_view->model()->columnCount(QModelIndex());
  12. //根据列号和行号获取最右下角的item的索引
  13. QModelIndex bottomRight = _table_view->model()->index(max_row-1, max_column-1, QModelIndex());
  14. //设置选择区域
  15. QItemSelection curSelection(topLeft, bottomRight);
  16. _table_view->selectionModel()->select(curSelection, QItemSelectionModel::Toggle);
  17. }

通过点击切换选择,可以实现选择区域的切换,因为我们设置选择的类型为Toggle,系统会将选中的变为取消选中,将取消选中的变为选中。
切换前
https://cdn.llfc.club/1671529103099.jpg
切换后
https://cdn.llfc.club/1671529287147.jpg
因为默认当前的条目是第1行1列,所以getCurrentItemData会打印他的信息。每一个model都有当前条目和选择条目两个属性。

捕获选择条目的变化

当我们点击鼠标选择一个条目时,当前条目就变为该条目,选择的条目也变为该条目。我们可以通过选择模型发出的selectionChanged信号,获取选择了那些条目的索引,以及取消选择了哪些条目的索引。我们可以通过currentChanged获取当前的条目索引以及变化之前的条目索引。现在MainWindow中添加两个槽函数的声明

  1. public slots:
  2. void updateSelection(const QItemSelection& selected, const QItemSelection& deselected);
  3. void changeCurrent(const QModelIndex& current, const QModelIndex& previous);

两个槽函数的参数和信号的参数匹配。我们实现changeCurrent,当选择模型的当前索引变化时,打印变化前后的索引行号,列号。

  1. void MainWindow::changeCurrent(const QModelIndex& current, const QModelIndex& previous){
  2. qDebug() << tr("move(%1, %2) to (%3, %4)").arg(previous.row())
  3. .arg(previous.column()).arg(current.row()).arg(current.column());
  4. }

然后我们实现选择模型的选择条目变化时更新条目的数据

  1. void MainWindow::updateSelection(const QItemSelection& selected, const QItemSelection& deselected){
  2. QModelIndex index;
  3. QModelIndexList list = selected.indexes();
  4. //为现在的选择项目设置值
  5. for(int i =0; i < list.size(); i++){
  6. QString text = QString("(%1, %2)").arg(list[i].row()).arg(list[i].column());
  7. _table_view->model()->setData(list[i], text);
  8. }
  9. list = deselected.indexes();
  10. foreach(index, list){
  11. _table_view->model()->setData(index,"");
  12. }
  13. }

在MainWindow的构造函数中添加信号和槽函数的链接

  1. //选择模型的选择条目更改后触发updateSelection函数
  2. connect(selection_model, &QItemSelectionModel::selectionChanged, this, &MainWindow::updateSelection);
  3. //选择模型的当前项目更改后,关联changeCurrent函数
  4. connect(selection_model, &QItemSelectionModel::currentChanged, this, &MainWindow::changeCurrent);

点击某个条目,其他条目的数据都变为空,这个条目被选中,内容修改为行列号
https://cdn.llfc.club/1671530820828.jpg

多个View共享模型

MVC结构最大的好处就是可以通过多个View共享同一个model,这个model的选中状态改变时,多个View的显示效果一致。
在构造函数中添加另一个QTableView

  1. QTableView * tableView2;
  2. tableView2 = new QTableView();
  3. tableView2->setWindowTitle("tableView2");
  4. tableView2->resize(400,300);
  5. tableView2->setModel(model);
  6. tableView2->setSelectionModel(selection_model);
  7. tableView2->show();

可以看到两个View显示的选中状态是一致的
https://cdn.llfc.club/1671531054879.jpg

源码链接

源码链接
https://gitee.com/secondtonone1/qt-learning-notes

热门评论

热门文章

  1. 解密定时器的实现细节

    喜欢(566) 浏览(2327)
  2. C++ 类的继承封装和多态

    喜欢(588) 浏览(3252)
  3. Linux环境搭建和编码

    喜欢(594) 浏览(7075)
  4. windows环境搭建和vscode配置

    喜欢(587) 浏览(1870)
  5. slice介绍和使用

    喜欢(521) 浏览(1945)

最新评论

  1. asio多线程模型IOServicePool Lion:线程池一定要继承单例模式吗
  2. 泛型算法的定制操作 secondtonone1:lambda和bind是C11新增的利器,善于利用这两个机制可以极大地提升编程安全性和效率。
  3. 类和对象 陈宇航:支持!!!!
  4. C++ 虚函数表原理和类成员内存分布 WangQi888888:class Test{ int m; int b; }中b成员是int,为什么在内存中只占了1个字节。不应该是4个字节吗?是不是int应该改为char。这样的话就会符合图上说明的情况
  5. 解决博客回复区被脚本注入的问题 secondtonone1:走到现在我忽然明白一个道理,无论工作也好生活也罢,最重要的是开心,即使一份安稳的工作不能给我带来事业上的积累也要合理的舍弃,所以我还是想去做喜欢的方向。

个人公众号

个人微信