Access to the list contained in the object c ++ by a program written in QML
-
I have worker class written in c++
#ifndef WORKERCPP_H #define WORKERCPP_H #include <QObject> #include<QList> #include <QtSql> struct data{ int value; int limit_down; int limit_up; }; class workercpp : public QObject { Q_OBJECT void timerEvent(QTimerEvent *event); QSqlDatabase db; public: QList<data> model; // I wont acces to this object from QML explicit workercpp(QObject *parent = 0); signals: void dbUpdate(); // signals qlist is updated public slots: }; #endif // WORKERCPP_H
I export worker object to qml
#include <QGuiApplication> #include <QQmlApplicationEngine> #include<QtQml> #include "workercpp.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<workercpp>("m_module",1,0,"Worker"); //register type with qml QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
Is posible acces to Qlist<data> object from qml?? This object I regular updates on the timer event
import QtQuick 2.6 import QtQuick.Window 2.2 import m_module 1.0 Window { visible: true width: Screen.width height: Screen.height Worker{ id: worker onDbUpdate: { //object is updated I need make vizu } } }
for any suggestions, I thank you
-
I have worker class written in c++
#ifndef WORKERCPP_H #define WORKERCPP_H #include <QObject> #include<QList> #include <QtSql> struct data{ int value; int limit_down; int limit_up; }; class workercpp : public QObject { Q_OBJECT void timerEvent(QTimerEvent *event); QSqlDatabase db; public: QList<data> model; // I wont acces to this object from QML explicit workercpp(QObject *parent = 0); signals: void dbUpdate(); // signals qlist is updated public slots: }; #endif // WORKERCPP_H
I export worker object to qml
#include <QGuiApplication> #include <QQmlApplicationEngine> #include<QtQml> #include "workercpp.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<workercpp>("m_module",1,0,"Worker"); //register type with qml QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
Is posible acces to Qlist<data> object from qml?? This object I regular updates on the timer event
import QtQuick 2.6 import QtQuick.Window 2.2 import m_module 1.0 Window { visible: true width: Screen.width height: Screen.height Worker{ id: worker onDbUpdate: { //object is updated I need make vizu } } }
for any suggestions, I thank you
@mateczek Just add a public slot in workercpp, e.g. QList<QVariant> getData(). However, it's a matter of type conversion between c++ and qml. See http://doc.qt.io/qt-5/qtqml-cppintegration-data.html. You can't use your data struct as is, instead use QList<int> or QVariantMap as described in docs, or create a simple QObject derived class with properties, or add those three integers as properties to you worker class.
Actually the most simple way would be to send them as signal arguments, then you don't need to add anything in you code except those arguments to the signal and emit signal() call. You can use the arguments directly by name in your qml code.
-
"QObject derived class with properties, or add those three integers as properties to you worker class."
I try this solution but, but I did something wrong:
My code on this momenet1 file QObject derived class :
#ifndef DATAOBJECT_H #define DATAOBJECT_H #include <QObject> class DataObject : public QObject { Q_OBJECT public: int _value; int _limit_down; int _limit_up; //QML need only read Data Q_PROPERTY(int value READ value) Q_PROPERTY(int limit_down READ limit_down) Q_PROPERTY(int limit_up READ limit_up) DataObject():QObject(){ _value=0; _limit_down=0; _limit_up=0; } DataObject(int val,int ldown,int lup):QObject(){ _value=val; _limit_down=ldown; _limit_up=lup; } DataObject(DataObject & source):QObject(){ _value=source._value; _limit_down=source._limit_down; _limit_up=source._limit_up; } int value(){return _value;} int limit_down(){return _limit_down;} int limit_up(){return _limit_up;} }; #endif // DATAOBJECT_H
2 file: header file worker class
#ifndef WORKERCPP_H #define WORKERCPP_H #include <QObject> #include<QList> #include <QtSql> #include<dataobject.h> class workercpp : public QObject { Q_OBJECT void timerEvent(QTimerEvent *event); QSqlDatabase db; public: QList<DataObject*> model; explicit workercpp(QObject *parent = 0); signals: void dbUpdate(QList<DataObject*> listObj); // this signals emitet list public slots: }; #endif // WORKERCPP_H
- implementation file worker class
#include "workercpp.h" void workercpp::timerEvent(QTimerEvent *event) { emit dbUpdate(model); // signal emit list } workercpp::workercpp(QObject *parent) : QObject(parent) { //add two object to list model.append(new DataObject(1,1,1)); model.append(new DataObject(2,2,2)); //start timer this->startTimer(1000); }
4 main.cpp file
#include <QGuiApplication> #include <QQmlApplicationEngine> #include<QtQml> #include "workercpp.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<workercpp>("m_module",1,0,"Worker"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
and last qml file
import QtQuick 2.6 import QtQuick.Window 2.2 import m_module 1.0 Window { visible: true width: Screen.width height: Screen.height Text{ width: 100 height: 20 anchors.centerIn: parent id: napis } Worker{ id: worker onDbUpdate:{ console.log(listObj[1].value) // not Work } } }
after start program My application send mesage :
qrc:/main.qml:19: TypeError: Cannot read property 'value' of undefined qrc:/main.qml:19: TypeError: Cannot read property 'value' of undefined
-
"QObject derived class with properties, or add those three integers as properties to you worker class."
I try this solution but, but I did something wrong:
My code on this momenet1 file QObject derived class :
#ifndef DATAOBJECT_H #define DATAOBJECT_H #include <QObject> class DataObject : public QObject { Q_OBJECT public: int _value; int _limit_down; int _limit_up; //QML need only read Data Q_PROPERTY(int value READ value) Q_PROPERTY(int limit_down READ limit_down) Q_PROPERTY(int limit_up READ limit_up) DataObject():QObject(){ _value=0; _limit_down=0; _limit_up=0; } DataObject(int val,int ldown,int lup):QObject(){ _value=val; _limit_down=ldown; _limit_up=lup; } DataObject(DataObject & source):QObject(){ _value=source._value; _limit_down=source._limit_down; _limit_up=source._limit_up; } int value(){return _value;} int limit_down(){return _limit_down;} int limit_up(){return _limit_up;} }; #endif // DATAOBJECT_H
2 file: header file worker class
#ifndef WORKERCPP_H #define WORKERCPP_H #include <QObject> #include<QList> #include <QtSql> #include<dataobject.h> class workercpp : public QObject { Q_OBJECT void timerEvent(QTimerEvent *event); QSqlDatabase db; public: QList<DataObject*> model; explicit workercpp(QObject *parent = 0); signals: void dbUpdate(QList<DataObject*> listObj); // this signals emitet list public slots: }; #endif // WORKERCPP_H
- implementation file worker class
#include "workercpp.h" void workercpp::timerEvent(QTimerEvent *event) { emit dbUpdate(model); // signal emit list } workercpp::workercpp(QObject *parent) : QObject(parent) { //add two object to list model.append(new DataObject(1,1,1)); model.append(new DataObject(2,2,2)); //start timer this->startTimer(1000); }
4 main.cpp file
#include <QGuiApplication> #include <QQmlApplicationEngine> #include<QtQml> #include "workercpp.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<workercpp>("m_module",1,0,"Worker"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
and last qml file
import QtQuick 2.6 import QtQuick.Window 2.2 import m_module 1.0 Window { visible: true width: Screen.width height: Screen.height Text{ width: 100 height: 20 anchors.centerIn: parent id: napis } Worker{ id: worker onDbUpdate:{ console.log(listObj[1].value) // not Work } } }
after start program My application send mesage :
qrc:/main.qml:19: TypeError: Cannot read property 'value' of undefined qrc:/main.qml:19: TypeError: Cannot read property 'value' of undefined
@mateczek I had some incorrect things as:
QList<DataObject*> model; //not work QList<QObject*> model; //work Q_PROPERTY(int value READ value) // not work Q_PROPERTY(int value READ value NOTIFY valueChanged) // ok!!!
full example, and solution I presented here: https://www.youtube.com/watch?v=L2JsguBzDEg