Qt:5.5新特性,TreeView使用(quick)

注:在5.6里面,delegate的Item获取styleData.value可以在Item构造的时候直接获取,无需去监听value的changed。

这在5.5里面是一个bug,5.6修复了这个问题。



~~~~~原文~~~~~

在5.5版本里面,quick新加入了TreeView,以前是没有的。

在这里我来介绍一下简单的使用。


效果:

Qt:5.5新特性,TreeView使用(quick)_第1张图片


上源码(一共6个文件)

main.cpp

#include 
#include 

#include "TreeModel.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("TreeModel", new TreeModel);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return a.exec();
}


main.qml

import QtQuick 2.4
import QtQuick.Controls 1.4

ApplicationWindow {
    title: qsTr("TreeView")
    width: 400
    height: 300
    visible: true

    TreeView {
        anchors.fill: parent

        Component.onCompleted: {
            model = TreeModel.model();
            print(model);
        }

        itemDelegate: Text {
            color: styleData.textColor
            elide: styleData.elideMode
            text: styleData.value

            onTextChanged: {
                print(styleData.value)
            }
        }

        TableViewColumn {
            title: "A"
            role: "text"
            width: 200
        }

        TableViewColumn {
            title: "B"
            role: "text2"
            width: 200
        }
    }
}

TreeItem.h

#ifndef TREEITEM_H
#define TREEITEM_H

// C lib import
#include 

// Qt lib import
#include 
#include 
#include 

class TreeItem
{
private:
    TreeItem *m_parentItem;
    QList m_childItems;
    QList m_itemData;

public:
    TreeItem(const QList &data);

    ~TreeItem();

    void appendChild(TreeItem *child);

    TreeItem *child(int row);

    int childCount() const;

    int columnCount() const;

    QVariant data(int column) const;

    int row() const;

    TreeItem *parent();

    void setParent(TreeItem *parent);
};
#endif

TreeItem.cpp

#include "TreeItem.h"

TreeItem::TreeItem(const QList &data)
{
    m_itemData = data;
}

TreeItem::~TreeItem()
{
    qDeleteAll(m_childItems);
}

void TreeItem::appendChild(TreeItem *item)
{
    item->setParent(this);
    m_childItems.append(item);
}

TreeItem *TreeItem::child(int row)
{
    return m_childItems.value(row);
}

int TreeItem::childCount() const
{
    return m_childItems.count();
}

int TreeItem::columnCount() const
{
    return m_itemData.count();
}

QVariant TreeItem::data(int column) const
{
    return m_itemData.value(column);
}

TreeItem *TreeItem::parent()
{
    assert(m_parentItem);
    return m_parentItem;
}

void TreeItem::setParent(TreeItem *parent)
{
    m_parentItem = parent;
}

int TreeItem::row() const
{
    if(!m_parentItem) { return 0; }

    return m_parentItem->m_childItems.indexOf(const_cast(this));
}

TreeModel.h

#ifndef TREEMODEL_H
#define TREEMODEL_H

// Qt lib import
#include 
#include 
#include 

#include "TreeItem.h"

class TreeModel: public QAbstractItemModel
{
    Q_OBJECT

private:
    TreeItem *m_rootItem;

public:
    TreeModel(QObject *parent = NULL);

    ~TreeModel();

    QVariant data(const QModelIndex &index, int role) const;

    Qt::ItemFlags flags(const QModelIndex &index) const;

    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;

    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;

    QModelIndex parent(const QModelIndex &index) const;

    int rowCount(const QModelIndex &parent = QModelIndex()) const;

    int columnCount(const QModelIndex &parent = QModelIndex()) const;

    QHash roleNames() const;

public slots:
    QAbstractItemModel *model();
};

#endif // TREEMODEL_H

TreeModel.cpp

#include "TreeModel.h"

TreeModel::TreeModel(QObject *parent):
    QAbstractItemModel(parent)
{
    m_rootItem = new TreeItem( { "A", "B" } );

    auto item1 = new TreeItem( { "a", "b" } );
    auto item2 = new TreeItem( { "aa", "bb" } );
    auto item3 = new TreeItem( { "aaa", "bbb" } );

    m_rootItem->appendChild(item1);
    item1->appendChild(item2);
    item2->appendChild(item3);
}

TreeModel::~TreeModel()
{
    delete m_rootItem;
}

int TreeModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return static_cast(parent.internalPointer())->columnCount();
    }
    else
    {
        return m_rootItem->columnCount();
    }
}

QHash TreeModel::roleNames() const
{
    /*
      roleName可以自己定义,我这里是随便写的
     */

    QHash names(QAbstractItemModel::roleNames());
    names[12345] = "text";
    names[12346] = "text2";
    return names;
}

QVariant TreeModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    switch(role)
    {
        case 12345:
        {
            return static_cast(index.internalPointer())->data(0);
        }
        case 12346:
        {
            return static_cast(index.internalPointer())->data(1);
        }
        case Qt::DisplayRole:
        {
            return static_cast(index.internalPointer())->data(index.column());
        }
        default:
        {
            return QVariant();
        }
    }
}

Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
    if(!index.isValid())
    {
        return 0;
    }

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        return m_rootItem->data(section);
    }

    return QVariant();
}

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(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(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(parent.column() > 0)
    {
        return 0;
    }

    if(!parent.isValid())
    {
        parentItem = m_rootItem;
    }
    else
    {
        parentItem = static_cast(parent.internalPointer());
    }

    return parentItem->childCount();
}

QAbstractItemModel *TreeModel::model()
{
    return this;
}


打包好的工程文件下载地址

http://download.csdn.net/detail/wsj18808050/8987179





你可能感兴趣的:(Qt)