Qt5 QML,为什么模型内部的模型导致缺少模型变量?

时间:2016-05-19 03:57:01

标签: qt qml qt5

尝试嵌套模型以生成一个列表,另一个模型作为外部列表中的行。它有点工作,但是当我重置外部模型时,我得到了损坏的数据。

这两个模型都是C ++。有一个外部模型,但对于每个列表项,将创建另一个模型。第二个是动态的。

main.qml:

import QtQuick 2.5
import QtQuick.Controls 1.4
import com.example.qml 1.0

Item
{
    id: thing1
    width: parent.width
    height: 150

    property int me
    property string name

    Column
    {
        width: parent.width;

        Label
        {
            width: parent.width
            height: 50
            text: name
        }

        Item
        {
            width: parent.width;
            height: 100

            // use the row model to space boxes on the row
            Repeater
            {
                model: myModel.getRowModel()
                delegate: Thing2 { x: model.position }
            }
        }
    }        
}

Thing1.qml

import QtQuick 2.5
import QtQuick.Controls 1.4

Item
{
    id: thing2
    width: 100
    height: 100

    Rectangle
    {
        anchors.fill: parent
        color: "blue"
    }
}

Thing2.qml

#include <QAbstractListModel>
#include <QQmlApplicationEngine>
#include <QObject>
#include <QString>
#include <QDebug>

class RowModel : public QAbstractListModel
{
    Q_OBJECT

public:

    int rowCount(const QModelIndex& parent = QModelIndex()) const override
    {
        return 3;
    }  

    QVariant data(const QModelIndex &index, int role) const override
    {
        // space out on the row
        int ix = index.row();
        return ix*110;
    }

    QHash<int, QByteArray> roleNames() const override
    {
        QHash<int, QByteArray> roles;
        roles[Qt::UserRole+1] = "position";
        return roles;
    }
};


class MyModel : public QAbstractListModel
{
    Q_OBJECT

public:

    int rowCount(const QModelIndex& parent = QModelIndex()) const override
    {
        return 3;
    }  

    QVariant data(const QModelIndex &index, int role) const override
    {
        int ix = index.row();
        if (ix < 1) return "Larry";
        if (ix < 2) return "Barry";
        return "Gary";
    }

    QHash<int, QByteArray> roleNames() const override
    {
        QHash<int, QByteArray> roles;
        roles[Qt::UserRole+1] = "name";
        return roles;
    }

    Q_INVOKABLE void changed()
    {
        beginResetModel();
        endResetModel();
    }

    Q_INVOKABLE RowModel* getRowModel()
    {
        // make a model for the row
        return new RowModel();
    }
};

C ++模型,mymodel.h

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

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;

        MyModel model;

        qmlRegisterType<RowModel>("com.example.qml", 1, 0, "RowModel");

        QQmlContext* ctxt = engine.rootContext();
        ctxt->setContextProperty("myModel",  &model);
        engine.addImportPath(":/.");
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        return app.exec();
    }

最后,main.cpp

qrc:/Thing1.qml:8: TypeError: Cannot read property of null
qrc:/main.qml:35:45: Unable to assign [undefined] to QString
qrc:/Thing1.qml:8: TypeError: Cannot read property of null
qrc:/main.qml:35:45: Unable to assign [undefined] to QString
qrc:/Thing1.qml:8: TypeError: Cannot read property of null
qrc:/main.qml:35:45: Unable to assign [undefined] to QString
qrc:/Thing1.qml:8: TypeError: Cannot read property of null
qrc:/main.qml:35:45: Unable to assign [undefined] to QString
qrc:/Thing1.qml:8: TypeError: Cannot read property of null
qrc:/main.qml:35:45: Unable to assign [undefined] to QString
qrc:/Thing1.qml:8: TypeError: Cannot read property of null
qrc:/main.qml:35:45: Unable to assign [undefined] to QString

这就是我所看到的:

running image

然后稍微调整窗口大小,您将获得以下内容:

model

main.qml中的JESS变量会以某种方式丢失并变为空。不确定这是怎么发生的。

这里是实际文件的主要链接, https://gist.github.com/anonymous/a837957207f08f73c4bd29f1d00843df

感谢您的帮助。

0 个答案:

没有答案