Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Class member data are not available (empty)

$
0
0
Hi, i am new to qml and i don’t know why the QList is empty, when the function printNumberOfValuesQml was called from qml. See my simple example code. // main.qml import QtQuick 2.2 import QtQuick.Controls 1.1 import com.test.demo 1.0     ApplicationWindow {     visible: true     width: 640     height: 480         MyClass{         id: myclass     }         Button {         id: button1         x: 280         y: 134         text: qsTr("Button")         onClicked: {               myclass.printNumberOfValuesQml()           }     } } // MyClass.h #ifndef MYCLASS_H #define MYCLASS_H   #include <QList> #include <QObject> #include <iostream>     class MyClass : public QObject{   Q_OBJECT   public:     Q_INVOKABLE void printNumberOfValuesQml();     void printNumberOfValuesCpp();     void init();   private:     QList<int> ints; };   #endif // MYCLASS_H // MyClass.cpp #include "MyClass.h"       void MyClass::printNumberOfValuesQml(){       std::cout << "number of items (qml) " << ints.size() << std::endl; }         void MyClass::printNumberOfValuesCpp(){       std::cout << "number of items (cpp) " << ints.size() << std::endl; }         void MyClass::init(){       ints.push_back(1); ints.push_back(2); ints.push_back(3); } // main.cpp #include <QApplication> #include <QQmlApplicationEngine> #include <QQmlComponent>   #include "MyClass.h"     int main(int argc, char *argv[]) {     QApplication app(argc, argv);       qmlRegisterType<MyClass>("com.test.demo", 1, 0, "MyClass");       QQmlApplicationEngine engine;     engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));       MyClass myClass;     myClass.init();     myClass.printNumberOfValuesCpp();       return app.exec(); } if the button clicked printNumberOfValuesQml prints number of items (qml) 0 Could someone explain that ? Thanks.

Viewing all articles
Browse latest Browse all 4972

Trending Articles