2、视图类
视图的概述:
视图包含了模型中的数据项,并将它们呈现给用户。
视图通常管理从模型获取数据的整体布局,它们可以自己渲染独立的数据项,也可以使用委托来处理渲染和编辑。
视图的特性:
视图还可以处理项目间的导航,以及项目选择的某些方面(选择行为,选择模式)。
视图可以实现一些基本的用户接口特性,如上下文菜单和拖放等
视图可以提供默认的编辑实现,也可以和委托一起来提供一个自定义的编辑器。
QTableView和QTreeView,可以显示表头。
Qt提供了QListView、QTableView、QTreeView 三个现成的视图。
视图中的项目选择:
视图中被选择的项目的信息存储在一个QItemSelectionModel实例中,这样被选择的项目模型索引便保持在一个独立的模型中,与所有的视图都是独立的。
选择由选择范围指定,只需要记录每一个选择范围开始和结束的模型索引即可。
选择可以看作是在选择模型中保存的一个模型索引集合,最近的项目选择被称为当前选择。
1、当前项目和被选择的项目
视图中总是有一个当前项目和一个被选择的项目,两者是两个独立的状态。
当操作选择时,可以将QItemSeletionModel看作一个项目模型中所有项目的选择状态的一个记录。
2、使用选择模型
一个视图的选择模型可以使用selectionModel()函数获得,在多个视图之间可以使用setSelectionModel()函数来共享该选择模型。
视图类中提供了几个比较方便的函数来选择,如:
seletColumn()选择指定的一列项目;
selectCoumns()选择指定的多列项目;
selectRow()选择指定的一行项目;
selectRows()选择指定的多行项目;
下面看一个实例:
该实例,是表格模型的选择处理。
相关类的定义如下,实现了表格数据项的访问,数据项选择的处理
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QTableView;
class QItemSelection;
class QModelIndex;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void getCurrentItemData();
void toggleSelection();
void updateSelection(const QItemSelection &selected,
const QItemSelection &deselected);
void changeCurrent(const QModelIndex ¤t, const QModelIndex &previous);
private:
Ui::MainWindow *ui;
QTableView *tableView;
QTableView *tableView2;
};
#endif // MAINWINDOW_H
具体成员函数如下:
#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 *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);
ui->mainToolBar->addAction(tr("now items"), this, SLOT(getCurrentItemData()));
ui->mainToolBar->addAction(tr("change selection"), this, SLOT(toggleSelection()));
connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(updateSelection(QItemSelection,QItemSelection)));
connect(selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(changeCurrent(QModelIndex,QModelIndex)));
// 多个视图共享选择
tableView2 = new QTableView;
tableView2->setWindowTitle("tableView2");
tableView2->resize(400, 300);
tableView2->setModel(model);
tableView2->setSelectionModel(selectionModel);
tableView2->show();
}
MainWindow::~MainWindow()
{
delete ui;
delete tableView2;
}
// 输出当前项目的内容
void MainWindow::getCurrentItemData()
{
qDebug() << tr("Items now is:")
<< 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 curSelection(topLeft, bottomRight);
tableView->selectionModel()->select(curSelection, QItemSelectionModel::Toggle);
}
// 更新选择
void MainWindow::updateSelection(const QItemSelection &selected,
const QItemSelection &deselected)
{
QModelIndex index;
QModelIndexList list = selected.indexes();
// 为现在选择的项目填充值
foreach (index, list) {
QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
tableView->model()->setData(index, text);
}
list = deselected.indexes();
// 清空上一次选择的项目的内容
foreach (index, list) {
tableView->model()->setData(index, "");
}
}
// 改变当前项目
void MainWindow::changeCurrent(const QModelIndex ¤t,
const QModelIndex &previous)
{
qDebug() << tr("move(%1,%2) to (%3,%4)")
.arg(previous.row()).arg(previous.column())
.arg(current.row()).arg(current.column());
}
C++ 的学习周期挺长的。Qt的学习周期也是很长,精通一门语言没有三五年的积累,很难说,就此可以独挡一面了。