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. Q_DECLARE_METATYPE(); issue loading QList<struct> in qml
Forum Updated to NodeBB v4.3 + New Features

Q_DECLARE_METATYPE(); issue loading QList<struct> in qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 3 Posters 1.3k Views 2 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.
  • M Offline
    M Offline
    MEMekaniske
    wrote on last edited by MEMekaniske
    #1

    Hello, I need to get a list of structs into a qml list or table, but I do not understand what I am doing wrong trying to get the list output to qml.

    I have declared the list and struct in a namespace, so that i can declare the metatype for the struct. This gives no errors, and I have no issues appending OurFiles structs to filesList or reading from the list itself in cpp.

    And there is no error from the declaration of the type or anything else, but it does not load, so I wonder, what have I missed or done wrong?

    namespace  {
        struct OurFiles {
            QString name;
            QString dateChanged;
            QString size;
        };
    }
    
    Q_DECLARE_METATYPE(OurFiles);
    

    in header class i have:

    Q_PROPERTY(QList<OurFiles> getFileList READ getFileListCpp)
    

    public:

    QList<OurFiles> filesList;
    QList<OurFiles> getFileListCpp() {return filesList;}
    

    and in source

    OurFiles fileItem;
        fileItem.name = name;
        fileItem.size = size;
        fileItem.dateChanged = date;
        filesList.append(fileItem);
    
    

    In qml i try to read like this

    console.log(backendEnd.getFileList)
    console.log(backendEnd.getFileList[1].name)
    

    But only get the result

    qrc:/WebServer.qml:19: TypeError: Cannot read property 'name' of undefined
    qml: []
    

    After change as described in post below, i have this output

    qml: [QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, )]
    qml: undefined
    

    Appreciate any help :)

    (Edit: backendEnd is also registred so that works fine..)

    KroMignonK 1 Reply Last reply
    0
    • M Offline
      M Offline
      MEMekaniske
      wrote on last edited by MEMekaniske
      #2

      Seems I misplaced the QList<OurFiles> filesList; declaration, moved it to inside the header class, but still some issues going on with loading,
      I have 8 files that's added, so number here adds up, but seems that I have done something wrong in the setup of the QList/Struct as now my output is:

      console.log(backendEnd.getFileList)
      console.log(backendEnd.getFileList[1].name)
      
      qml: [QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, )]
      qml: undefined
      
      1 Reply Last reply
      0
      • M MEMekaniske

        Hello, I need to get a list of structs into a qml list or table, but I do not understand what I am doing wrong trying to get the list output to qml.

        I have declared the list and struct in a namespace, so that i can declare the metatype for the struct. This gives no errors, and I have no issues appending OurFiles structs to filesList or reading from the list itself in cpp.

        And there is no error from the declaration of the type or anything else, but it does not load, so I wonder, what have I missed or done wrong?

        namespace  {
            struct OurFiles {
                QString name;
                QString dateChanged;
                QString size;
            };
        }
        
        Q_DECLARE_METATYPE(OurFiles);
        

        in header class i have:

        Q_PROPERTY(QList<OurFiles> getFileList READ getFileListCpp)
        

        public:

        QList<OurFiles> filesList;
        QList<OurFiles> getFileListCpp() {return filesList;}
        

        and in source

        OurFiles fileItem;
            fileItem.name = name;
            fileItem.size = size;
            fileItem.dateChanged = date;
            filesList.append(fileItem);
        
        

        In qml i try to read like this

        console.log(backendEnd.getFileList)
        console.log(backendEnd.getFileList[1].name)
        

        But only get the result

        qrc:/WebServer.qml:19: TypeError: Cannot read property 'name' of undefined
        qml: []
        

        After change as described in post below, i have this output

        qml: [QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, )]
        qml: undefined
        

        Appreciate any help :)

        (Edit: backendEnd is also registred so that works fine..)

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #3

        @MEMekaniske Please take time to read documentation, this will avoid you many frustrations!

        Custom data type exchange with QML is only possible with QObject sub-classes. This is mandatory.
        ==> https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        M 1 Reply Last reply
        1
        • KroMignonK KroMignon

          @MEMekaniske Please take time to read documentation, this will avoid you many frustrations!

          Custom data type exchange with QML is only possible with QObject sub-classes. This is mandatory.
          ==> https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html

          M Offline
          M Offline
          MEMekaniske
          wrote on last edited by
          #4

          @KroMignon said in Q_DECLARE_METATYPE(); issue loading QList<struct> in qml:
          Custom data type exchange with QML is only possible with QObject sub-classes. This is mandatory.

          ==> https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html

          Ok, so it cannot be done.

          KroMignonK 1 Reply Last reply
          0
          • M MEMekaniske

            @KroMignon said in Q_DECLARE_METATYPE(); issue loading QList<struct> in qml:
            Custom data type exchange with QML is only possible with QObject sub-classes. This is mandatory.

            ==> https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html

            Ok, so it cannot be done.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @MEMekaniske said in Q_DECLARE_METATYPE(); issue loading QList<struct> in qml:

            Ok, so it cannot be done.

            No the way you try to do it. QML needs QObject to work, that's the deal ;)

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            1
            • M Offline
              M Offline
              MEMekaniske
              wrote on last edited by
              #6

              I understand.

              But the struct needs to be above all else to to regisred, and to use Q_OBJECT or GADGET and set struct properties that will not be private I need to be under where I can registrer the type, and then that wont work. So I don't really get it. My real issue is probably just finding out how to register the types "anywhere"..

              But it took 4-5 minutes rewriting it all to use QList<QStringList> and that works just fine.. Just very confusing not being able to use struct the same "normal" way..

              Thanks though :)

              KroMignonK 1 Reply Last reply
              0
              • M MEMekaniske

                I understand.

                But the struct needs to be above all else to to regisred, and to use Q_OBJECT or GADGET and set struct properties that will not be private I need to be under where I can registrer the type, and then that wont work. So I don't really get it. My real issue is probably just finding out how to register the types "anywhere"..

                But it took 4-5 minutes rewriting it all to use QList<QStringList> and that works just fine.. Just very confusing not being able to use struct the same "normal" way..

                Thanks though :)

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @MEMekaniske said in Q_DECLARE_METATYPE(); issue loading QList<struct> in qml:

                But it took 4-5 minutes rewriting it all to use QList<QStringList> and that works just fine.. Just very confusing not being able to use struct the same "normal" way..

                QStringList is a known native type in QML, your struct not.
                You can register struct to work with signals/slots mechanisms, but not with QML. To add new type to QML, the type must be a QObject to enable QML to work with.
                There are plenty of examples how to do it.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                M 1 Reply Last reply
                0
                • KroMignonK KroMignon

                  @MEMekaniske said in Q_DECLARE_METATYPE(); issue loading QList<struct> in qml:

                  But it took 4-5 minutes rewriting it all to use QList<QStringList> and that works just fine.. Just very confusing not being able to use struct the same "normal" way..

                  QStringList is a known native type in QML, your struct not.
                  You can register struct to work with signals/slots mechanisms, but not with QML. To add new type to QML, the type must be a QObject to enable QML to work with.
                  There are plenty of examples how to do it.

                  M Offline
                  M Offline
                  MEMekaniske
                  wrote on last edited by
                  #8

                  @KroMignon Yeah I understood :)

                  I did not find any suitable examples for my case. But I will figure it out..

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Aida Mosayebi
                    wrote on last edited by
                    #9

                    you must define struct above and outside the class definition as follows

                    struct OurFiles{
                    Q_GADGET

                    public:
                    QString name;
                    QString dateChanged;
                    QString size;

                     Q_PROPERTY(QString name MEMBER name)
                     Q_PROPERTY(QString dateChanged MEMBER dateChanged)
                     Q_PROPERTY(QString size MEMBER size)
                    

                    };

                    Q_DECLARE_METATYPE(OurFiles)

                    class Test : public QObject {
                    Q_OBJECT
                    // your code
                    }
                    and you must add Q_OBJECT macro to your class.

                    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