QT

Model / View框架:视图选择

2017-04-27  本文已影响0人  托尼章

概述

Current Item Selected Item
只能有一个当前项 可以同时有多个被选中项
当前项可以被鼠标或者按键导航改变 被选中项是通过set 或者unset来改变的,同时也要根据其他预设模式来决定--例如 单选,多选等。
按下F2或者双击鼠标左键当前项会被编辑(开启了编辑模式) **
当前项由焦点矩形显示 选中项由选择矩形显示

示例

// 建立模型和视图
TableModel* model = new TableModel(8,4,&app);
QTableView* view = new QTableView(this);
view->setModel(model);       

// 获取选择模型
QItemSelectionModel* selectionModel = view->selectionModel();

// 构造选择区域
QModelIndex topLeft = model->index(0,0,QModelIndex());
QModelIndex bottomRight = model->index(5,2,QModelIndex());
QItemSelection selection(topLeft,bottomRight);

// 选中项目
selectionModel->select(selection,QItemSelectionModel::Select);

以上代码的执行结果如下图:

selected_item.png
上一篇下一篇

猜你喜欢

热点阅读