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 :)