Qt ModelView 学习笔记 联系客服

发布时间 : 星期三 文章Qt ModelView 学习笔记更新完毕开始阅读2545926cda38376baf1faec0

QItemSelectionModel *selectionModel = table->selectionModel(); QModelIndex topLeft; QModelIndex bottomRight;

topLeft = model->index(0, 0, QModelIndex()); bottomRight = model->index(5, 2, QModelIndex());

QItemSelection selection(topLeft, bottomRight);

selectionModel->select(selection, QItemSelectionModel::Select); 结果如下:

读取选择状态

存储在选择模型中indexes可以用selectionIndexes()函数来读取。它返回一个未排序的model indexes列表,我们可以遍历它,如果我们知道他们关联于哪个model的话。

QModelIndexList indexes = selectionModel->selectedIndexes(); QModelIndex index; foreach(index, indexes) {

QString text = QString(\ model->setData(index, text); }

选择模型在选择发生变化时会发出信号。这用于通知别的组件包括整体与当前焦点项所发生的变化。我们可以连接selectionChanged()信号到一个槽,检查当信号产生时哪些项

被选择或被取消选择。这个槽被调用时带有两个参数,它们都是QItemSelection对象,一个包含新被选择的项,另一个包含新近被取消选择的项。下面的代码演示了给新选择的项添加数据内容,新近被取消选择的项的内容被清空。

void MainWindow::updateSelection(const QItemSelection &selected, const QItemSelection &deselected) {

QModelIndex index;

QModelIndexList items = selected.indexes(); foreach (index, items) {

QString text = QString(\ model->setData(index, text); }

items = deselected.indexes(); foreach (index, items) model->setData(index, \ }

也可以通过响应currentChanged()信号来跟踪当前焦点项.对应的槽就有两个接收参数,一个表示之前的焦点,另一个表示当前的焦点。

void MainWindow::changeCurrent(const QModelIndex ¤t, const QModelIndex &previous) {

statusBar()->showMessage(

tr(\ .arg(previous.row()).arg(previous.column()) .arg(current.row()).arg(current.column())); } 更新选择

选择指令是通过选择标志提供的,它被定义在QItemSelectionModel::SelectionFlag中。常用的有Select标记,Toggle标记,Deselect标记,Current标记,Clear标记,其意义一目了然。沿上面例子的结果执行以下代码:

QItemSelection toggleSelection;

topLeft = model->index(2, 1, QModelIndex()); bottomRight = model->index(7, 3, QModelIndex()); toggleSelection.select(topLeft, bottomRight);

selectionModel->select(toggleSelection, QItemSelectionModel::Toggle); 结果如下:

缺省情况下,选择指令只针对单个项(由model indexes指定)。然而,选择指令可以通过与另外标记的结合来改变整行和整列。举例来说,假如你只使用一个index来调用select(),但是用Select标记与Rows标记的组合,那么包括那个项的整行都将被选择。看以下示例:

QItemSelection columnSelection;

topLeft = model->index(0, 1, QModelIndex()); bottomRight = model->index(0, 2, QModelIndex()); columnSelection.select(topLeft, bottomRight); selectionModel->select(columnSelection,

QItemSelectionModel::Select | QItemSelectionModel::Columns); QItemSelection rowSelection;

topLeft = model->index(0, 0, QModelIndex());

bottomRight = model->index(1, 0, QModelIndex()); rowSelection.select(topLeft, bottomRight); selectionModel->select(rowSelection,

QItemSelectionModel::Select | QItemSelectionModel::Rows); 结果如下

选择模型中所有项

为了选择model中的所有项,必须先得创建一个选择,它包括当前层次上的所有项: QModelIndex topLeft = model->index(0, 0, parent);

QModelIndex bottomRight = model->index(model->rowCount(parent)-1, model->columnCount(parent)-1, parent); QItemSelection selection(topLeft, bottomRight);

selectionModel->select(selection, QItemSelectionModel::Select); 顶级index可以这样:

QModelIndex parent = QModelIndex();

对具有层次结构的model来说,可以使用hasChildren()函数来决定给定项是否是其它项的父项。

Qt Model/View 学习笔记 (七) 清源游民 gameogre@gmail.com