视图中被选择的项目的信息存储在一个QItemSelectionModel中,这样被选择的索引被保持在一个独立的模型中,与所有的视图都是独立的。
当前项目和被选择的项目.png
标准的视图类中提供了默认的选择模型。
属于一个视图的选择模型可以使用selectionModel()来获得,
而且还可以在多个视图之间使用setSelectionModel()函数来共享选择模型。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QTableView;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QTableView *tableView;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardItemModel>
#include <QTableView>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStandardItemModel *model = new QStandardItemModel(7,4,this);
for(int row = 0;row<7;++row){
for(int column = 0;column<4;++column){
QStandardItem *item = new QStandardItem(QString("%1").arg(row*4+column));
model ->setItem(row,column,item);
}
}
tableView = new QTableView;
tableView -> setModel(model);
setCentralWidget(tableView);
//试图中被选择的项目的信息存储在一个QItemSelectionModel中,这样被选择的索引被保持在一个独立的模型中,与所有的视图都是独立的。
//获取视图的项目选择模型
QItemSelectionModel *selectionModel =tableView -> selectionModel();
//定义左上角和右下角的索引,然后使用俩个索引创建选择
QModelIndex topLeft;
QModelIndex bottomRight;
topLeft = model ->index(1,1,QModelIndex());
bottomRight = model ->index(5,2,QModelIndex());
QItemSelection selection(topLeft,bottomRight);
//使用指定的选择模式来选择项目
selectionModel ->select(selection , QItemSelectionModel::Select);
//选中 指定所选择的项目的所有行的项目。
//selectionModel ->select(selection, QItemSelectionModel::Select|QItemSelectionModel::Rows);
/*
这里先获取了视图的选择模型;
要使用选择模型来选择视图中的项目,那么就必须指定 QItemSelection 和 选择模式 QItemSelectionModel::SelectionFlag。
QItemSelection 是一个项目选择块,需要指定它的左上角和右下角的项目索引;
而选择模式是选择模型更新时的一种方式,它是一个枚举变量,在QItemSelectionModel类中被定义,可以在帮助中察看它的值。
这里QItemSelectionModel::Select 表示所指定的索引都将被选择;
QItemSelectionModel::Toggle 表明指定索引的当前状态切换为相反的状态,如果项目以前没有被选择,那么现在被选择。
QItemSelectionModel::SelectionFlag的值还可以使用位或运算符来使用,
*/
}
MainWindow::~MainWindow()
{
delete ui;
}
![](https://img.haomeiwen.com/i4602306/0a5bf64f33ee86e6.png)
下面来QItemSelectionModel::Toggle的效果:
在mianwindow.h中添加
public slots:
void getCurrentItemData();
void toggleSelection();
然后在cpp中的构造函数添加
ui->mainToolBar ->addAction(tr("当前项目"),this, SLOT(getCurrentItemData()));
ui->mainToolBar ->addAction(tr("切换选择"),this, SLOT(toggleSelection()));
void MainWindow::getCurrentItemData()
{
qDebug()<<tr("获取当前的内容:")
<<tableView -> selectionModel()->currentIndex().data().toString();
}
void MainWindow::toggleSelection()
{
QModelIndex topLeft = tableView ->model()->index(0,0,QModelIndex());
QModelIndex bottomRight = tableView ->model()->index(tableView ->model()->rowCount(QModelIndex())-1,tableView ->model()->columnCount(QModelIndex())-1,QModelIndex());
QItemSelection currentSelection(topLeft,bottomRight);
tableView ->selectionModel() ->select(currentSelection,QItemSelectionModel::Toggle);
}
![](https://img.haomeiwen.com/i4602306/e89d4fb5122495ba.png)
![](https://img.haomeiwen.com/i4602306/9bcede137040a46a.png)
要获取选择模型中的索引,可以使用selectedIndex()函数,它会返回一个模型索引的列表,然后便利列表即可。
当选择模型中选择的项目改变时候,会发射相关信号。
网友评论