Quantcast
Channel: Installation and Deployment
Viewing all articles
Browse latest Browse all 22112

exposing QQmlListProperty to QML : registering QQmlListProperty in QML type system using qmlRegisterType method

$
0
0
I’m trying to access a QList<MyOject> from QML so I’m using QQmlListProperty here is a part of the code : the class MyObject code : MyObject.h #ifndef MYOBJECT_H #define MYOBJECT_H   #include "track.h" #include <QObject> #include <QtQml/qqml.h>   class MyObject : public QObject{      Q_OBJECT    Q_PROPERTY(QQmlListProperty<Track> tracks READ tracks)   public:     explicit MyObject(QObject *parent = 0);     QQmlListProperty<Track> tracks();       private:     QList<Track*> Tracks;     static void append_track(QQmlListProperty<Track> *list, Track *track);       -- }; the class MyObject : MyObject.cpp QQmlListProperty <Track> MyObject::tracks() {     return QQmlListProperty<Track>(this, 0,&MyObject::append_track,                                          QQmlListProperty<Track>::CountFunction(),                                          QQmlListProperty<Track>::AtFunction(),                                          QQmlListProperty<Track>::ClearFunction()); } void MyObject::append_track(QQmlListProperty<Track> *list, Track *track) {      MyObject*obj = qobject_cast<MyObject*>(list->object);              obj->Tracks.append(track); } and in the main.cpp #include <QApplication> #include <QDeclarativeView> #include <QDeclarativeContext> #include <QtQml/QQmlEngine> #include "MyObject.h" #include "track.h"     Q_DECL_EXPORT int main(int argc, char *argv[]) {       QScopedPointer<QApplication> app(createApplication(argc, argv));       MyObject *myobj  = foo();   //initialisation of the main object         qmlRegisterType<Track>("type.base",1,0,"track");     Track track;     QDeclarativeView view;     QDeclarativeContext *context = view.rootContext();     context->setContextProperty("MyObject",myobj);     context->setContextProperty("track",&track);     view.setSource(QUrl::fromLocalFile("qml/Myproject/main.qml"));     view.show();       return app->exec();   } So when compiling i have 4 error : In function `QQmlPrivate::QQmlElement<Track>::~QQmlElement()’: error : undefined reference to `QQmlPrivate::qdeclarativeelement_destructor(QObject*)’ In function `QQmlPrivate::QQmlElement<Track>::~QQmlElement()’: error : undefined reference to `QQmlPrivate::qdeclarativeelement_destructor(QObject*)’ In function `int qmlRegisterType<Track>(char const*, int, int, char const*)’: undefined reference to `QQmlPrivate::qmlregister(QQmlPrivate::RegistrationType, void*)’ ld returned 1 exit status Note : just before facing this problem I had some issues : I’m not using QmlApplicationViewer and I use QDeclarativeView and QDeclarativeContext instead because I couldn’t access the setContextProperty() method from a QDeclarativeView viewer. So if you have a trick for this please share :). in the QQmlListProperty <Track> MyObject::tracks() method i couldn’t just return : return QQmlListProperty<Track>(this, 0,&MyObject::append_track); because i had this error :  In member function 'QQmlListProperty<Track> MyObject::tracks()':error : no matching function for call to 'QQmlListProperty<Track>::QQmlListProperty(MyObject* const, int, void (*) (QQmlListProperty<Track>*, track*))' candidates are : ... so I added : return QQmlListProperty<Track>(this, 0,&MyObject::append_track,                                          QQmlListProperty<Track>::CountFunction(),                                          QQmlListProperty<Track>::AtFunction(),                                          QQmlListProperty<Track>::ClearFunction()); but I don’t know if what I did is a convenient way to fix this problem? basically what I want to do is to access a string element from the list tracks from QML by doing something like : import QtQuick 1.1   Rectangle {     width: 360     height: 360     Text {           text: qsTr(MyObject.tracks.at(..))         anchors.centerIn: parent     }       MouseArea {         anchors.fill: parent         onClicked: {             Qt.quit();         }     } } thank you for your help !

Viewing all articles
Browse latest Browse all 22112

Trending Articles