I want to expose a QStringlist from C ++ to Qml and get access to its elements and their methods from the side of QML.
Here is what I have done so far:
this .h file for a class called manager.
#include <QObject> #include <QStringList> #include <QList> class Manager : public QObject { Q_OBJECT Q_PROPERTY(QStringList imagesPaths READ imagesPaths) Q_PROPERTY(QStringList imagesPaths READ imagesPaths2) Q_PROPERTY(QList<QStringList> imagesPathsLists READ imagesPathsLists) public: explicit Manager(QObject *parent = 0); QStringList imagesPaths() const; QStringList imagesPaths2() const; QList<QStringList> imagesPathsLists()const; signals: public slots: private: QStringList m_imagesPaths; QStringList m_imagesPaths2; QList<QStringList> m_imagesPathsLists; };
This is my .CPP file for implementing class methods
#include "manager.h" Manager::Manager(QObject *parent) : QObject(parent) { m_imagesPaths << "one" << "two" << "three" << "four"; m_imagesPaths2 << "one-2" << "two-2" << "three-2" << "four-2"; m_imagesPathsLists << m_imagesPaths << m_imagesPaths2; } QStringList Manager::imagesPaths() const { return m_imagesPaths; } QStringList Manager::imagesPaths2() const { return m_imagesPaths2; } QList<QStringList> Manager::imagesPathsLists() const { return m_imagesPathsLists; }
And the main.cpp file that contains the registration for my class
#include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" #include <qqmlcontext.h> #include "manager.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; Manager *mng = new Manager(); QQmlContext *ctxt = viewer.rootContext(); ctxt->setContextProperty("Manager",mng); viewer.setMainQmlFile(QStringLiteral("qml/listOfLists/main.qml")); viewer.showExpanded(); return app.exec(); }
Finally, a .Qml file that tries to retrieve data from lists
import QtQuick 2.0 Rectangle { width: 360 height: 360 MouseArea { anchors.fill: parent onClicked: { for(var i = 0; i < Manager.imagesPathsLists.count(); i++){ for(var j = 0; j < Manager.imagesPathsLists[i].count(); j++){ console.log(Manager.imagesPathsLists[i].at(j)) } } } } }
Whenever I click on the rectangle, I get the following error
QMetaProperty::read: Unable to handle unregistered datatype 'QList<QStringList>' for property 'Manager::imagesPathsLists' file:///E:/DevWork/build-listOfLists-Desktop_Qt_5_2_1_MinGW_32bit-Debug/qml/listOfLists/main.qml:10: TypeError: Cannot call method 'count' of undefined
I have been trying to solve this problem for two days now. I tried QQmlListProperty , but without success I don't know what I messed up.