您的位置:首页 > 编程语言 > Qt开发

QtCreator 模块/视图编程( 二) 项目选择,QItemSelectionModel

2014-07-19 11:30 302 查看
1 基本概念

QAbstractItemModel提供了一个标准模型接口,一个标准视图接口以及使用了模型索引提供的一种通用的方法来表示数据

QTableView和QTreeView在显示项目的同时还可以显示表头,这是通过QHeaderView类实现的,通过QAbstractItemModel::headerData()函数从模型中获取数据,然后使用一个标签来显示表头信息。

2 处理项目选择

2.1 视图中被选择的项目的信息存储在一个QItemSelectionModel实例中。被选择的项目模型索引与所有的视图都是独立的

一个模型设置多个视图时,可以实现在多个视图之间共享选择

视图中总有一个当前项目和一个被选择的项目,他们可以是同一个项目

2.2 使用选择模型

2.2.1 在主界面头文件中

class QTableView;


private:
QTableView * tableView;//定义视图私有变量


2.2.2 在界面实现文件中构造函数中添加下面代码

2.2.2.1 构建标准项目模型并设置数据

//构建项目模型并且添加数据
QStandardItemModel *model = new QStandardItemModel(7,4,this);//构建7行4列的项目模型
for (int row=0; row < 7; row++) {
for (int col=0; col < 4; col++) {
QStandardItem* item = new QStandardItem(QString("%1").arg(row*4+col));
model->setItem(row,col,item);//为项目模型添加项目
}
}


2.2.2.2 添加表视图,并设置模型

//添加视图,并将视图放在窗体中央

tableView = new QTableView;

tableView->setModel(model);//为视图添加模型

setCentralWidget(tableView);

2.2.2.3 获取项目选择模型

//获取视图的项目选择模型

QItemSelectionModel * selectionModel = tableView->selectionModel();

2.2.2.4 设置被选择的项目

//定义左上,右下的索引,然后使用这两个索引创建选择

QModelIndex topLeft;

QModelIndex rightBottom;

QModelIndex parentIndex = QModelIndex();

topLeft = model->index(1,1,parentIndex);

rightBottom = model->index(5,2,parentIndex);

QItemSelection selection(topLeft, rightBottom);//设置被选择的项目

2.2.2.5 设置选择时的选择模式

//使用指定的选择模式来选择项目

selectionModel->select(selection,QItemSelectionModel::Select);

QItemSelectionModel::Toggle


选择模式

选择模型的选择改变信号

selectionChanged():会发送被选择的QItemSelectio和未被选择的QItemSelection

currentChanged():会发送当前选择的QModelIndex和前一个QModelIndex

在头文件中添加前置声明

class QItemSelection;

class QModelIndex;

添加槽函数

void updateSelection(QItemSelection &selected,QItemSelection &deselected);//选择模型改变后的槽函数

void changeCurrent(const QModelIndex ¤t,const QModelIndex &previous);//显示当前选择项的数据改变情况

构造函数中添加信号与槽的关联

connect(selectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),

this,SLOT(updateSelection(QItemSelection&,QItemSelection&)));

connect(selectionModel,SIGNAL(currentChanged(QModelIndex,QModelIndex)),

this,SLOT(changeCurrent(QModelIndex,QModelIndex)));

实现槽函数

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

{

QModelIndex index;

QModelIndexList indexList = selected.indexes();

foreach (index, indexList) {

QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());

tableView->model()->setData(index,text);

}

indexList = deselected.indexes();


//清空上一次选择的项目内容

foreach (index, indexList) {

tableView->model()->setData(index,"");

}

}


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

{

qDebug() << QString("move(%1,%2)to(%3,%4)")

.arg(previous.row()).arg(previous.column())

.arg(current.row()).arg(current.column());

}


2.2.3 视图间共享选择

需要把不同的视图设置相同的模型,就可以共享选择了

2.2.3.1 在界面头文件中添加私有变量

QTableView * tableView2;

2.2.3.2 构造函数中设置共享选择模型

tableView2 = new QTableView;

tableView2->setWindowTitle("TableView2");

tableView2->setModel(model);//设置模型

tableView2->setSelectionModel(selectionModel);//设置共同的选择模型

tableView2->show();


2.2.3.3 在析构函数中释放tableView2

delete tableView2;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: