【问题标题】:Using C++ model in QML, white page在 QML 中使用 C++ 模型,白页
【发布时间】:2016-05-30 09:38:28
【问题描述】:

我正在尝试将对象 Cloud 从 C++ 发送到 QML。我想使用模型属性在 QML 中显示我的云的名称。 我没有编译错误,但是当我执行代码时会写入多个信息:

  • QQUickView 仅支持加载从 快捷物品

为此,我尝试更改 QuickItem 中的所有 QObject,但没有成功。

  • 如果您的示例使用的是 QML2,并且您加载的 .qml 文件有“import QTquick1,0”或“import QT 4,7”,则会出现此错误

我寻找导入信号,但我找不到任何 QtQuick1,0 或 QT4 7 在我的代码中。

这里是我的 Cloud.h:

#ifndef CLOUD_H
#define CLOUD_H

#include <QObject>

class Cloud: public QObject
{
     Q_OBJECT
     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
     Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged)

public:
    Cloud(QObject *parent=0);
    Cloud(const QString &name, const QString &description, QObject *parent=0);

    QString name() const;
    void setName(const QString &name);

    QString description() const;
    void setDescription(const QString &name);

signals:
    void nameChanged();
    void descriptionChanged();

private:
    QString m_name;
    QString m_description;
};

#endif // CLOUD_H

我的云.cpp

#include "cloud.h"
#include <QDebug>


Cloud::Cloud(QObject *parent)
    :QObject(parent)
{

}

Cloud::Cloud(const QString &name, const QString &description, QObject *parent)
    :QObject(parent), m_name(name), m_description(description)
{
}
QString Cloud::name() const{
    return m_name;
}
void Cloud::setName(const QString &name){

    if(name != m_name){
        m_name = name;
        emit nameChanged();
    }
}
QString Cloud::description() const{
    return m_description;
}
void Cloud::setDescription(const QString &description){
    if(description != m_description){
        m_description = description;
        emit descriptionChanged();
    }
}

我的 main.cpp

#include <QGuiApplication>

#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include <QQmlContext>

#include "cloud.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    qmlRegisterType<Cloud>("Sky", 1,0,"Cloud");


    QList<Cloud*> cloudList;
    cloudList.append(new Cloud("Cumulus Mediocris", "super nuage brocoli"));
    cloudList.append(new Cloud("Cumulus Towering", "super nuage tour"));
    cloudList.append(new Cloud("Cumulonimbus", "Gros nuage pas content"));

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(cloudList));

    view.setSource(QUrl("qrc:main.qml"));
    view.show();

    return app.exec();
}

最后是我的 main.qml:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2


ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true
    Rectangle{
        width:10;height:10
        color:"red"
        anchors.left : parent.left
        anchors.top:parent.top
    }

    ListView{
        width:100;height:100

        model: myModel
        delegate:Rectangle{
            height:25
            width:100
            color:"pink"
            Text{text:model.modelData.name}
        }
    }
}

为了编写这段代码,我查看了Models and views in QtQuickUsing C++ Models with QtQUick View

提前感谢您的帮助:)

【问题讨论】:

    标签: c++ qt qml


    【解决方案1】:

    问题在于您的主要功能。 QQuickView 是在窗口中显示 QML 场景的快捷方式(QQuickView 本身是 QQuickWindow 的子类)。在您的 QML 中,根对象是 ApplicationWindow。不能在窗口中显示窗口。

    解决办法是切换到QQmlApplicationEngine

    QQmlApplicationEngine view;
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(cloudList));
    view.load(QUrl("qrc:main.qml"));
    

    第二个问题与您的模型有关。 ListView 会理解 QList&lt;QObject*&gt;,但不会理解 QList&lt;Cloud*&gt;。只需将声明更改为:

    QList<QObject*> cloudList;
    

    【讨论】:

    • 好的,我明白了,谢谢。这是否意味着如果不是将我的 ListView 写入 main.qml,而是将其写入另一个 qml 文件,它会起作用吗?
    • 不,问题是您的 QML 中的根元素是 ApplicationWindow。如果你使用它应该可以工作,例如Rectangle
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多