Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 这是QML中最小可行的TreeView模型吗?_Qt_Qml_Treeview_Qt5.9 - Fatal编程技术网

Qt 这是QML中最小可行的TreeView模型吗?

Qt 这是QML中最小可行的TreeView模型吗?,qt,qml,treeview,qt5.9,Qt,Qml,Treeview,Qt5.9,我正在做一个由三个项目组成的折叠列表:“嘿”,“什么”和“向上?”。我想把它放到树视图中。我知道此列表将仅包含这三个项目。因此,我想知道如何将这些项目“嵌套”在一起 我知道有一些敏捷系统的实现支持添加和删除父/子对象,查找索引。。。强大的模型。但是,我实际上只需要在可展开/可折叠视图中显示这些项目。以下是我读过的关于C++和QAbstractItemModels的文章: 其依据是: 通过Qt本身(实际上不适用于TreeView。但适用于QTreeView!) 下面是使用模型实现tree

我正在做一个由三个项目组成的折叠列表:“嘿”,“什么”和“向上?”。我想把它放到树视图中。我知道此列表将仅包含这三个项目。因此,我想知道如何将这些项目“嵌套”在一起

我知道有一些敏捷系统的实现支持添加和删除父/子对象,查找索引。。。强大的模型。但是,我实际上只需要在可展开/可折叠视图中显示这些项目。以下是我读过的关于C++和QAbstractItemModels的文章:

  • 其依据是:
  • 通过Qt本身(实际上不适用于TreeView。但适用于QTreeView!)
下面是使用模型实现treeview的最简单可行代码:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4


Window {
    id: mywindow
    visible: true
    width: 640
    height: 480

    TreeView {
        id: treeview
        anchors.fill: parent

        TableViewColumn {
            title: "Phrase"
            role: "phrase"
        }
        model: phraseModel
    }

    ListModel {
        id: phraseModel
        ListElement { phrase: "Hey"; }
        ListElement { phrase: "What's"; }
        ListElement { phrase: "Up?"; }
    }
}
我希望输出产生如下嵌套堆栈:

Hey
    What's
        Up?
但我将所有内容都放在一列中,并相互对齐:

Hey
What's
Up?

我知道我还没有指定家长,我也不完全确定该怎么做——但我甚至不确定是否需要对这段代码这样做。所以我的问题是:将这三个元素堆叠到可扩展/可折叠视图中的最后一步是什么?

没有可以使用TreeView的本机QML模型,因此我实现了一个尝试通用的模型:

树元素

//treeelement.h
#ifndef TreeElement_H
#定义树元素
#包括
#包括
类树元素:公共QObject
{
Q_对象
公众:
Q_属性(QQmlListProperty项读取项)
Q_CLASSINFO(“默认属性”、“项目”)
TreeElement(QObject*parent=Q_NULLPTR);
Q_可调用树元素*parentItem()常量;
布尔插入项(TreeElement*项,int pos=-1);
QQmlListProperty项();
TreeElement*子(整数索引)常量;
无效清除();
Q_可调用int pos()常量;
Q_可调用int count()常量;
私人:
静态void元素(QQmlListProperty*属性,TreeElement*值);
静态int countElement(qqmlistproperty*property);
静态无效clearElement(QQmlListProperty*属性);
静态树元素*子元素(QQmlListProperty*属性,int索引);
QList m_childs;
TreeElement*m_父项;
};
#endif//TreeElement_H
//treeelement.cpp
#包括“treeelement.h”
TreeElement::TreeElement(QObject*父对象):
QObject(父对象),
m_父(nullptr){}
TreeElement*TreeElement::parentItem()常量{
返回母校;
}
QQmlListProperty TreeeElement::items(){
返回qqmlistproperty(此,
这
&TreeElement::appendElement,
&TreeElement::countElement,
&树元素::卫星,
&TreeElement::clearElement);
}
TreeElement*TreeElement::子(整数索引)常量{
如果(索引<0 | |索引>=m|u childs.length())
返回空ptr;
返回m_childs.at(索引);
}
void树元素::clear(){
qDeleteAll(m_childs);
m_childs.clear();
}
布尔树元素::插入项(树元素*项,整数位置){
如果(pos>m_childs.count())
返回false;
如果(位置<0)
pos=m_childs.count();
item->m_parent=此项;
item->setParent(此项);
m_childs.插页(位置、项目);
返回true;
}
int treeeElement::pos()常量{
TreeElement*父项=父项();
如果(家长)
返回parent->m_childs.indexOf(const_cast(this));
返回0;
}
int treeeElement::count()常量{
返回m_childs.size();
}
void TreeElement::appendElement(QQmlListProperty*属性,TreeElement*值){
TreeElement*parent=qobject\u cast(属性->对象);
父项->插入项(值);
}
int TreeeElement::countElement(QQmlListProperty*属性){
TreeElement*parent=qobject\u cast(属性->对象);
返回父项->计数();
}
void TreeElement::clearElement(QQmlListProperty*属性){
TreeElement*parent=qobject\u cast(属性->对象);
父->清除();
}
TreeElement*TreeElement::atElement(QQmlListProperty*属性,int索引){
TreeElement*parent=qobject\u cast(属性->对象);
如果(索引<0 | |索引>=parent->count())
返回空ptr;
返回父级->子级(索引);
}
树模型

//treemodel.h
#ifndef TreeModel_H
#定义树模型
#包括
#包括
类树元素;
类TreeModel:公共QAbstractItemModel
{
Q_对象
公众:
Q_属性(QQmlListProperty项读取项)
Q_属性(QVariantList角色读取角色写入设置角色通知角色更改)
Q_CLASSINFO(“默认属性”、“项目”)
树模型(QObject*parent=Q_NULLPTR);
~TreeModel()覆盖;
QHash roleNames()常量Q_DECL_覆盖;
QVariant数据(常量QModelIndex和index,int角色)常量Q_DECL_覆盖;
Qt::ItemFlags标志(常量QModelIndex和index)常量Q_DECL_覆盖;
QModelIndex索引(int行,int列,常量QModelIndex&parent=QModelIndex())常量Q_DECL_覆盖;
QModelIndex父项(常量QModelIndex&index)常量Q_DECL_覆盖;
int rowCount(const QModelIndex&parent=QModelIndex())const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex&parent=QModelIndex())const Q_DECL_OVERRIDE;
QQmlListProperty项();
QVariantList角色()常量;
无效集合角色(常量QVariantList和角色);
Q_可调用QModelIndex索引元素(TreeElement*项);
Q_可调用bool insertElement(treeeelement*项,常量QModelIndex&parent=QModelIndex(),int pos=-1);
TreeElement*elementFromIndex(常数QModelIndex&index)常数;
私人:
TreeElement*m_根;
QHash m_角色;
信号:
无效角色更改();
};
#endif//TreeModel_H
//treemodel.cpp
#包括“treemodel.h”
#包括“treeelement.h”
TreeModel::TreeModel(QObject*父对象):
QAbstracteModel(父级){
m_根=新树元素;
}
TreeModel::~TreeModel(){
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 1.4

import foo 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    TreeModel {
        id: treemodel
        roles: ["phrase"]
        TreeElement{
            property string phrase: "Hey"
            TreeElement{
                property string phrase: "What's"
                TreeElement{
                    property string phrase: "Up?"
                }
            }
        }
    }
    TreeView {
        anchors.fill: parent
        model: treemodel
        TableViewColumn {
            title: "Name"
            role: "phrase"
            width: 200
        }
    }
}
// CollapsibleGroupBox.qml
Item {
    property alias contentItem: content.contentItem;
    property string title: ""
    Item {
        id: titleBar
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        height: 30
        Row {
            anchors.fill: parent
            CheckBox {
                Layout.alignment: Qt.AlignLeft
                id: expand
                checked: true;
            }
            Text {
                Layout.alignment: Qt.AlignLeft
                text: title
            }
        }
    }
    Pane {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: titleBar.bottom
        anchors.bottom: parent.bottom
        topPadding: 0
        visible: expand.checked
        id: content
    }
}
// Main.qml
Item {
    height: 500
    width: 500
    CollapsibleGroupBox {
        anchors.fill: parent
        title: "Hey!"
        contentItem: CollapsibleGroupBox {
            title: "What's"
            contentItem: CollapsibleGroupBox {
                title: "up?"
            }
        }
    }
}
import QtQuick 2.9
import QtQuick.Window 2.2
import UISettings 1.0
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 1.4 as SV



Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Flickable {
        id: flick
        anchors.fill: parent
        clip: true
        contentHeight: col.implicitHeight
        property var mymodel: {
            "animals": {
                "big": {
                    "land": "elephants",
                    "water": "whales"
                },
                "small": {
                    "land": "mice",
                    "water": "fish"
                }
            },
            "plants": {
                "trees": "evergreens"
            }
        }

        Column {
            id: col
            Component.onCompleted: componentListView.createObject(this, {"objmodel":flick.mymodel});
        }

        Component {
            id: componentListView
            Repeater {
                id: repeater
                property var objmodel: ({})
                model: Object.keys(objmodel)

                ColumnLayout {
                    Layout.leftMargin: 50
                    Button {
                        property var sprite: null
                        text: modelData
                        onClicked: {
                            if(sprite === null) {
                                if(typeof objmodel[modelData] === 'object')
                                sprite = componentListView.createObject(parent, {"objmodel":objmodel[modelData]});
                            }
                            else
                                sprite.destroy()

                        }
                    }
                }
            }
        }
    }
}