1

I'am trying to send a object Cloud from C++ to QML. I want to show the name of my cloud in the QML using the model property. I have no compilation error but when I execute the code severals infos are wrote :

  • QQUickView only supports loading of root objects that derive from QuickItem

for this one i've tried to change all my QObject in QuickItem, without success.

  • If your example is using QML2, and the .qml file you loaded has 'import QTquick1,0' or 'import QT 4,7', this error will occur

I look for the import signaled, but I can't find any QtQuick1,0 or QT4 7 in my code.

Here my 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

My Cloud.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();
    }
}

My 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();
}

And finally my 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}
        }
    }
}

To write this code I took a look to Models and views in QtQuick and Using C++ Models with QtQUick View

Thank you in advance for your help :)

1 Answer 1

1

The problem is in your main function. QQuickView is a quick way to display a QML scene in a window (QQuickView is itself a subclass of QQuickWindow). In your QML, the root object is ApplicationWindow. You cannot display a window inside a window.

The solution is to switch to QQmlApplicationEngine:

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

The second problem is with your model. ListView would understand a QList<QObject*>, but not a QList<Cloud*>. Just change the declaration to:

QList<QObject*> cloudList;
Sign up to request clarification or add additional context in comments.

2 Comments

Ok I understand now, thank you. Does that mean it would work if instead of writting my ListView in the main.qml, I would write it in an other qml file ?
No, the problem is that the root element in your QML is ApplicationWindow. It should work if you used e.g. Rectangle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.