簡體   English   中英

QML 從嵌套 ZA8CFDE6331BD59EB2AC96F8911C4B6666 中的父組件訪問 C++ model

[英]QML access C++ model from parent component in nested object

我有一個 C++ 列表 model,我可以在其中正確讀取數據

model.h

class Animal
{
public:
    Animal(const QString &type, const QString &size);
//![0]

    QString type() const;
    QString size() const;

private:
    QString m_type;
    QString m_size;
//![1]
};

class AnimalModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum AnimalRoles {
        TypeRole = Qt::UserRole + 1,
        SizeRole
    };

    AnimalModel(QObject *parent = 0);
//![1]

    void addAnimal(const Animal &animal);

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

    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

protected:
    QHash<int, QByteArray> roleNames() const;
private:
    QList<Animal> m_animals;
//![2]
};

model.cpp

#include "model.h"

Animal::Animal(const QString &type, const QString &size)
    : m_type(type), m_size(size)
{
}

QString Animal::type() const
{
    return m_type;
}

QString Animal::size() const
{
    return m_size;
}

AnimalModel::AnimalModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

void AnimalModel::addAnimal(const Animal &animal)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_animals << animal;
    endInsertRows();
}

int AnimalModel::rowCount(const QModelIndex & parent) const {
    Q_UNUSED(parent);
    return m_animals.count();
}

QVariant AnimalModel::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() >= m_animals.count())
        return QVariant();

    const Animal &animal = m_animals[index.row()];
    if (role == TypeRole)
        return animal.type();
    else if (role == SizeRole)
        return animal.size();
    return QVariant();
}

//![0]
QHash<int, QByteArray> AnimalModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[TypeRole] = "type";
    roles[SizeRole] = "size";
    return roles;
}
//![0]

主文件

#include "model.h"

#include <QGuiApplication>
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>

//![0]
int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);

    AnimalModel model;
    model.addAnimal(Animal("Wolf", "Medium"));
    model.addAnimal(Animal("Polar bear", "Large"));
    model.addAnimal(Animal("Quoll", "Small"));

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setInitialProperties({{"model", QVariant::fromValue(&model)}});
//![0]
    view.setSource(QUrl("qrc:view.qml"));
    view.show();

    return app.exec();
}

如果我想將父組件添加到我的列表視圖,model 將無法再訪問,因為它的可見性僅來自父 object。 那我該如何暴露 model 呢?

import QtQuick 2.0
import QtQuick.Window 2.15

//![0]
Window{
id: root

// how to catch here the model?

visible: true
width: 640
height: 480

ListView {
    anchors.fill: parent
    model: root.model // does not work

    delegate: Text {
        required property string type
        required property string size

        text: "Animal: " + type + ", " + size
    }
}

}

好吧,這真是個有趣的錯誤。 首先,根據Qt 文檔,屬性定義為:

[default] [required] [readonly] property <propertyType> <propertyName>

如您所見,關鍵字property是必需的。

另一個問題是 setInitialProperties 說:注意:您只能使用此 function 來初始化頂級屬性。 . 因此,從根項(以及model屬性)中移動ListView會使該屬性無法訪問。

還有一個問題是ListView已經有一個名為model的屬性,重新定義的嘗試是不正確的。 因此,您必須添加一些具有model屬性的根項目,這將解決問題。 當然,您需要將ListView.model綁定到此屬性

還有一個小例子:)

Item {
    id: root
    width: 600
    height: 400
    property var model

    ListView {
        model: root.model
        delegate: Text {
            text: "Animal: " + type + ", " + size
        }
    }
}

Window不能成為根項目,因為您使用QQuickView

在根元素中創建一個具有相同名稱的別名屬性 view.setInitialProperties 允許一切正常工作

主文件

view.setInitialProperties({{"mylistmodel", QVariant::fromValue(&model)}});

查看.qml

import QtQuick 2.0
import QtQuick.Window 2.15

Window{
id: root
property alias mylistmodel : listView.model

visible: true
width: 640
height: 480

ListView {
    id: listView
    anchors.fill: parent

    required model

    delegate: Text {
        required property string type
        required property string size

        text: "Animal: " + type + ", " + size
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM