本实例来自Qt 官方的一个实例(Editable Tree Model Example)
简介:
本实例是关于怎样基于模式视图框架下的 树模型的实现。
该模型支持可编辑的表单项,自定义表头,删除插入行和列,也可以插入子表单项。
在标准数据模型中提供了这些函数:
flags()、data()、headerData()、columnCount()、rowCount()。
因为本实例还具有继承的关系,所以还需要index()、parent()两个函数。
一个可编辑的模型还需要实现setData()、和setHeaderData(),同时还需要一个状态机制flags()。
由于该实例还需要实现行列变化,所以我们还需要实现insertRows()、insertColumns()、removeRows()、removeColumn()。
设计:
1、树状数据模型的内部关系:
从上图中可以看到:树状数据模型中的每一数据项都包含它的父项(parent())和子项(child())信息。
每一个数据项(itemData)依据行和列信息进行存储。我们可以通过简单的data()函数来遍历整个数据项(itemData list);通过setData()来修改数据项(itemData)
根节点(root Item)相当于一个空数据索引,用QModelIndex()来指定。
树状数据模型的访问:
如图:数据项可以通过行和列以及它的父项信息进行访问:
QVariant a = model->index(0, 0, QModelIndex()).data();
QVariant b = model->index(1, 0, QModelIndex()).data();
树状数据模型的存储结构:
数据项的存储类似QVariant 对象。
通过数据索引来构建数据项的内在关系:
(Relating items using model indexes)
树状模型数据项的类定义:
class TreeItem
{
public:
explicit TreeItem(const QVector<QVariant> &data, TreeItem *parent = nullptr);
~TreeItem();
TreeItem *child(int number);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
bool insertChildren(int position, int count, int columns);
bool insertColumns(int position, int columns);
TreeItem *parent();
bool removeChildren(int position, int count);
bool removeColumns(int position, int columns);
int childNumber() const;
bool setData(int column, const QVariant &value);
private:
QVector<TreeItem*> childItems;
QVector<QVariant> itemData;
TreeItem *parentItem;
};
树状数据模型类的成员实现:
1、构造函数: 构建数据项
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
: itemData(data),
parentItem(parent)
{}
2、析构函数: 确保当一个数据项删除的时候,那么它的所有子项都会被删除。
TreeItem::~TreeItem()
{
qDeleteAll(childItems);
}
3、parent(),每个数据项都存有指针指向它的父项
TreeItem *TreeItem::parent()
{
return parentItem;
}
4、三个函数提供关于子项的信息:
child()、childCount()、childNumber()
TreeItem *TreeItem::child(int number)
{
if (number < 0 || number >= childItems.size())
return nullptr;
return childItems.at(number);
}
int TreeItem::childCount() const
{
return childItems.count();
}
int TreeItem::childNumber() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
return 0;
}
5、columnCount() :
int TreeItem::columnCount() const
{
return itemData.count();
}
6、获取数据data():
QVariant TreeItem::data(int column) const
{
if (column < 0 || column >= itemData.size())
return QVariant();
return itemData.at(column);
}
7、修改数据
bool TreeItem::setData(int column, const QVariant &value)
{
if (column < 0 || column >= itemData.size())
return false;
itemData[column] = value;
return true;
}
8、子节点数据的插入
bool TreeItem::insertChildren(int position, int count, int columns)
{
if (position < 0 || position > childItems.size())
return false;
for (int row = 0; row < count; ++row) {
QVector<QVariant> data(columns);
TreeItem *item = new TreeItem(data, this);
childItems.insert(position, item);
}
return true;
}
9、子节点数据的删除:
bool TreeItem::removeChildren(int position, int count)
{
if (position < 0 || position + count > childItems.size())
return false;
for (int row = 0; row < count; ++row)
delete childItems.takeAt(position);
return true;
}
10、插入列:
bool TreeItem::insertColumns(int position, int columns)
{
if (position < 0 || position > itemData.size())
return false;
for (int column = 0; column < columns; ++column)
itemData.insert(position, QVariant());
for (TreeItem *child : qAsConst(childItems))
child->insertColumns(position, columns);
return true;
}
树状模型类的定义:
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
TreeModel(const QStringList &headers, const QString &data,
QObject *parent = nullptr);
~TreeModel();
//The constructor and destructor are specific to this model.
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
//Read-only tree models only need to provide the above functions. The following public functions provide support for editing and resizing:
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
bool setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role = Qt::EditRole) override;
bool insertColumns(int position, int columns,
const QModelIndex &parent = QModelIndex()) override;
bool removeColumns(int position, int columns,
const QModelIndex &parent = QModelIndex()) override;
bool insertRows(int position, int rows,
const QModelIndex &parent = QModelIndex()) override;
bool removeRows(int position, int rows,
const QModelIndex &parent = QModelIndex()) override;
private:
void setupModelData(const QStringList &lines, TreeItem *parent);
TreeItem *getItem(const QModelIndex &index) const;
TreeItem *rootItem;
};
树状模型类的成员实现:
1、构造函数: 创建根节点和表头数据项
TreeModel::TreeModel(const QStringList &headers, const QString &data, QObject *parent)
: QAbstractItemModel(parent)
{
QVector<QVariant> rootData;
for (const QString &header : headers)
rootData << header;
rootItem = new TreeItem(rootData);
setupModelData(data.split('\n'), rootItem);
}
2、析构函数:
TreeModel::~TreeModel()
{
delete rootItem;
}
3、获取数据项:
TreeItem *TreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item)
return item;
}
return rootItem;
}
4、行统计:
int TreeModel::rowCount(const QModelIndex &parent) const
{
const TreeItem *parentItem = getItem(parent);
return parentItem ? parentItem->childCount() : 0;
}
5、列统计:
int TreeModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return rootItem->columnCount();
}
6、flags()
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
7、创建数据索引
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0)
return QModelIndex();
TreeItem *parentItem = getItem(parent);
if (!parentItem)
return QModelIndex();
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
return QModelIndex();
}
8、parent()
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *childItem = getItem(index);
TreeItem *parentItem = childItem ? childItem->parent() : nullptr;
if (parentItem == rootItem || !parentItem)
return QModelIndex();
return createIndex(parentItem->childNumber(), 0, parentItem);
}
主函数类:
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "ui_mainwindow.h"
#include <QMainWindow>
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
public slots:
void updateActions();
private slots:
void insertChild();
bool insertColumn();
void insertRow();
bool removeColumn();
void removeRow();
};
#endif // MAINWINDOW_H
主函数成员实现:
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "mainwindow.h"
#include "treemodel.h"
#include <QFile>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
const QStringList headers({tr("Title"), tr("Description")});
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel *model = new TreeModel(headers, file.readAll());
file.close();
view->setModel(model);
for (int column = 0; column < model->columnCount(); ++column)
view->resizeColumnToContents(column);
connect(exitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &MainWindow::updateActions);
connect(actionsMenu, &QMenu::aboutToShow, this, &MainWindow::updateActions);
connect(insertRowAction, &QAction::triggered, this, &MainWindow::insertRow);
connect(insertColumnAction, &QAction::triggered, this, &MainWindow::insertColumn);
connect(removeRowAction, &QAction::triggered, this, &MainWindow::removeRow);
connect(removeColumnAction, &QAction::triggered, this, &MainWindow::removeColumn);
connect(insertChildAction, &QAction::triggered, this, &MainWindow::insertChild);
updateActions();
}
void MainWindow::insertChild()
{
const QModelIndex index = view->selectionModel()->currentIndex();
QAbstractItemModel *model = view->model();
if (model->columnCount(index) == 0) {
if (!model->insertColumn(0, index))
return;
}
if (!model->insertRow(0, index))
return;
for (int column = 0; column < model->columnCount(index); ++column) {
const QModelIndex child = model->index(0, column, index);
model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
if (!model->headerData(column, Qt::Horizontal).isValid())
model->setHeaderData(column, Qt::Horizontal, QVariant(tr("[No header]")), Qt::EditRole);
}
view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
QItemSelectionModel::ClearAndSelect);
updateActions();
}
bool MainWindow::insertColumn()
{
QAbstractItemModel *model = view->model();
int column = view->selectionModel()->currentIndex().column();
// Insert a column in the parent item.
bool changed = model->insertColumn(column + 1);
if (changed)
model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
updateActions();
return changed;
}
void MainWindow::insertRow()
{
const QModelIndex index = view->selectionModel()->currentIndex();
QAbstractItemModel *model = view->model();
if (!model->insertRow(index.row()+1, index.parent()))
return;
updateActions();
for (int column = 0; column < model->columnCount(index.parent()); ++column) {
const QModelIndex child = model->index(index.row() + 1, column, index.parent());
model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
}
}
bool MainWindow::removeColumn()
{
QAbstractItemModel *model = view->model();
const int column = view->selectionModel()->currentIndex().column();
// Insert columns in each child of the parent item.
const bool changed = model->removeColumn(column);
if (changed)
updateActions();
return changed;
}
void MainWindow::removeRow()
{
const QModelIndex index = view->selectionModel()->currentIndex();
QAbstractItemModel *model = view->model();
if (model->removeRow(index.row(), index.parent()))
updateActions();
}
void MainWindow::updateActions()
{
const bool hasSelection = !view->selectionModel()->selection().isEmpty();
removeRowAction->setEnabled(hasSelection);
removeColumnAction->setEnabled(hasSelection);
const bool hasCurrent = view->selectionModel()->currentIndex().isValid();
insertRowAction->setEnabled(hasCurrent);
insertColumnAction->setEnabled(hasCurrent);
if (hasCurrent) {
view->closePersistentEditor(view->selectionModel()->currentIndex());
const int row = view->selectionModel()->currentIndex().row();
const int column = view->selectionModel()->currentIndex().column();
if (view->selectionModel()->currentIndex().parent().isValid())
statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
else
statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
}
}
总结:花了一个上午的时间分析了这个树状数据模式的视图实现,抽象数据原本就相对难理解,这个模型和数据类的定义内容还是挺多的,需要逐步消化。