Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Class member data are not available (empty)

Class member data are not available (empty)

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dr_X
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dr_X
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3685
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dr_X
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              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)

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • T Offline
                T Offline
                t3685
                wrote on last edited by
                #7

                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]

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dr_X
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved