QML树控件TreeView的使⽤(上)
在Qt5.5之前是没有树控件的,我们在使⽤时⽤的是ListView来构造出⼀个树,Qt5.5之后的QML开发阶段,有了树控件TreeView,本篇着重记录QML的TreeView的使⽤。根据MVC分解⽂件(类)如下:
TreeController.h TreeController.cpp
TreeModel.h TreeModel.cpp
TreeItem.c TreeItem.cpp
各个类职能划分如下:
TreeController主要负责⽤户的操作,例如加载qml⽂件,⽤户主动加载数据,主动删除数据,主动插⼊数据等。TreeModel主要存放数据(TreeItem为TreeModel的成员,也是存放数据的)。QML的TreeView主要显⽰数据。
重点在于QML的TreeView与TreeModel如何交互将数据正确显⽰出来。
TreeModel.h头⽂件如下:
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
enum ItemRoles {
NAME = Qt::UserRole + 1,
SIMPLIFY
};
public:
TreeModel(QObject *parent = NULL);
~TreeModel();
void appendChild(const QModelIndex& index);
bool removeRows(int row, int count, QModelIndex parent);
QModelIndex parent(const QModelIndex &index) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QHash<int, QByteArray> roleNames() const;
private:
TreeItem *m_rootItem;
};
TreeItem.h头⽂件如下:
#ifndef TREEITEM_H
#define TREEITEM_H
#include <QList>
#include <QVariant>
#include <QStringList>
#include <QModelIndex>
class TreeItem
{
public:
TreeItem::TreeItem();
TreeItem(const QList<QVariant> &data, TreeItem* parent);
~TreeItem();
void appendChild(TreeItem *child);
控件的使用void deleteAllChild();
TreeItem *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
TreeItem *parent();
void setParent(TreeItem *parent);
private:
TreeItem *m_parentItem;
QList<TreeItem*> m_childItems;
QList<QVariant> m_itemData;
};
#endif
可以看出TreeModel是继承⾃QAbstractItemModel,其中QVariant data(const QModelIndex &index, int role) const;成员函数和QHash<int, QByteArray> roleNames() const;成员函数⽐较关键,是QML读取C++数据的关键函数。
TreeModel.cpp源⽂件如下:
TreeModel::TreeModel(QObject *parent) :
QAbstractItemModel(parent), m_rootItem(NULL)
{
m_rootItem = new TreeItem;
QList<QVariant> list;
list.append("ZhongGuo");
list.append("ZhG");
auto item = new TreeItem(list, m_rootItem);
m_rootItem->appendChild(item);
QList<QVariant> BJ_List;
BJ_List.append("BeiJing");
BJ_List.append("BJ");
auto BJ_Item = new TreeItem(BJ_List, item);
item->appendChild(BJ_Item);
QList<QVariant> ShX_List;
ShX_List.append("ShannXi");
ShX_List.append("ShX");
QList<QVariant> XiAn_List;
XiAn_List.append("XiAn");
XiAn_List.append("XA");
QList<QVariant> XiAn_GaoXin_List;
QList<QVariant> XiAn_GaoXin_List;
XiAn_GaoXin_List.append("GaoXin");
XiAn_GaoXin_List.append("XA_GaoXin");
auto ShX_Item = new TreeItem(ShX_List, item);
auto XA_Item = new TreeItem(XiAn_List, ShX_Item);
auto XA_GX_Item = new TreeItem(XiAn_GaoXin_List, XA_Item);
item->appendChild(ShX_Item);
ShX_Item->appendChild(XA_Item);
XA_Item->appendChild(XA_GX_Item);
QList<QVariant> GuangDong_List;
GuangDong_List.append("GuangDong");
GuangDong_List.append("GD");
QList<QVariant> DongGuan;
DongGuan.append("DongGuan");
DongGuan.append("DG");
auto GuangDong_Item = new TreeItem(GuangDong_List, item);
auto DongGuan_Item = new TreeItem(DongGuan, GuangDong_Item);
item->appendChild(GuangDong_Item);
GuangDong_Item->appendChild(DongGuan_Item);
QList<QVariant> ShangHai;
ShangHai.append("ShangHai");
ShangHai.append("ShH");
auto ShangHai_Item = new TreeItem(ShangHai, item);
item->appendChild(ShangHai_Item);
}
TreeModel::~TreeModel()
{
delete m_rootItem;
}
int TreeModel::columnCount(const QModelIndex &parent) const
{
return 2;
//返回实际的列数 (实际是他返回了0,因为根节点⽤的是⽆参的构造),TreeView控件会认为表是空表,不获取数据 if (parent.isValid())
{
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
}
else
{
return m_rootItem->columnCount();
}
}
QHash<int, QByteArray> TreeModel::roleNames() const
{
QHash<int, QByteArray> names(QAbstractItemModel::roleNames());
names[NAME] = "name";
names[SIMPLIFY] = "simplify";
return names;
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
switch (role)
{
case NAME:
{
return static_cast<TreeItem*>(index.internalPointer())->data(0);
}
}
case SIMPLIFY:
{
return static_cast<TreeItem*>(index.internalPointer())->data(1);
}
}
}
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
{
return 0;
}
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
if (!hasIndex(row, column, parent))
{
return QModelIndex();
}
TreeItem *parentItem;
if (!parent.isValid())
{
parentItem = m_rootItem;
}
else
{
parentItem = static_cast<TreeItem*>(parent.internalPointer());
}
TreeItem *childItem = parentItem->child(row);
if (childItem)
{
return createIndex(row, column, childItem);
}
else
{
return QModelIndex();
}
}
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
{
return QModelIndex();
}
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == m_rootItem)
{
return QModelIndex();
}
return createIndex(parentItem->row(), 0, parentItem);
}
int TreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (lumn() > 0)
{
return 0;
return 0;
}
if (!parent.isValid())
{
parentItem = m_rootItem;
}
else
{
parentItem = static_cast<TreeItem*>(parent.internalPointer()); }
return parentItem->childCount();
}
以上即为TreeModel的源⽂件。
TreeItem.cpp源⽂件如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。