日志
QModelIndex
2012-10-20 10:19
The QModelIndex class is used to locate data in a data model.The index is used by item views, delegates, and selectionmodels to locate an item in the model.
Aselection model belonging to one view can be obtained using the view's selectionModel() function, and sharedbetween many views with setSelectionModel(),so the construction of new selection models is generally not required. weconstruct an instance of a custom table model with 32 items in total, and opena table view onto its data. The table view's default selection model isretrieved for later use: TableModel *model = new TableModel(8,4, &app); QTableView *table = new QTableView(0); table->setModel(model); QItemSelectionModel *selectionModel = table->selectionModel(); Weselect the model indexes corresponding to the top-left and bottom-right itemsin the region to be selected: QModelIndextopLeft; QModelIndexbottomRight; topLeft= model->index(0, 0, QModelIndex()); bottomRight= model->index(5, 2, QModelIndex()); To select these items in the model, and seethe corresponding change in the table view, we need to construct a selectionobject then apply it to the selection model: QItemSelectionselection(topLeft, bottomRight); selectionModel-> select(selection,QItemSelectionModel::Select); Themodel indexes stored in the selection model can be read using theselectedIndexes() function. QModelIndexListindexes = selectionModel->selectedIndexes(); QModelIndexindex; foreach(index,indexes) { QString textQString("(%1,%2)").arg(index.row()).arg(index.column()); model->setData(index, text);[size=font-size:12.0pt,12.0pt][font=&]} |
下一篇: Qt5 C++源码中使用中文的简单步骤
上一篇: 无