Class member data are not available (empty)



  • 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.



  • Hi, you defined two MyClass instances in your code. Once in your main.cpp and once in your main.qml. You called init only on your instance in your main.cpp. Hope this helps



  • Ahh, of course. Thanks.

    My real problem is actually another. I try to exposing a c++ QList<QObject*> model for a qml listview.

    If i create MyClass in the main.cpp, code like this works fine
    @class DataObject : public QObject
    {
    Q_OBJECT

     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
     Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
     ...
    

    };

    int main(int argc, char ** argv)
    {
    QApplication app(argc, argv);
    QmlApplicationViewer viewer;

     QList<QObject*> dataList;
     dataList.append(new DataObject("Item 1", "red"));
     dataList.append(new DataObject("Item 2", "green"));
     dataList.append(new DataObject("Item 3", "blue"));
     dataList.append(new DataObject("Item 4", "yellow"));
    
     QDeclarativeContext *ctxt = viewer.rootContext();
     ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
     ...
    

    @

    But if i instances MyClass (only) in qml, i could not get the model data in the ListView's delegate.



  • you use setContextProperty you need to refer to the object by the name you have given it, namely "myModel". Again making another qml instance of MyClass won't do you any good.



  • bq. you use setContextProperty you need to refer to the object by the name you have given it, namely “myModel”. Again making another qml instance of MyClass won’t do you any good.

    I have understood this. I have just one instance of MyClass in c++ and use setContextProperty...

    But i have no idea how could do this if the instance of a class was made in qml. In this case can't use setContextProperty to work with my model, or?



  • You are looking for exposing the model written in C++ and exposing to QML, you can refer the examples give in your installation in following directory.
    examples\declarative\modelviews\stringlistmodel

    Also if you would like to expose list from C++ and QML, check for
    Q_PROPERTY(QQmlListProperty<Person> guests READ guests)



  • In this case you can use the id of the qml instance. Of course you need to make sure it his data

    [quote author="dr_X" date="1406271683"]bq. you use setContextProperty you need to refer to the object by the name you have given it, namely “myModel”. Again making another qml instance of MyClass won’t do you any good.

    I have understood this. I have just one instance of MyClass in c++ and use setContextProperty...

    But i have no idea how could do this if the instance of a class was made in qml. In this case can't use setContextProperty to work with my model, or? [/quote]



  • Many thanks for the answers. I will try it and report.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.