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. How to pass an array (list) filled with custom data to QML?

How to pass an array (list) filled with custom data to QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 374 Views
  • 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.
  • A Offline
    A Offline
    Anton1978
    wrote on 24 Sept 2024, 18:20 last edited by
    #1

    Good afternoon.
    I want to pass a QML list filled with custom class. It seems that this is not so difficult if you make a list of pointers to objects.
    They even receive this list in QML, but they cannot understand it.
    Custom class

    #include <QObject>
    #include <qqml.h>
    
    class MinePlace : public QObject
    {
        Q_OBJECT
        QML_ELEMENT
    
    public:
        explicit MinePlace();
        int mark = 0;
        int bomb = 0;
        int x = 0;
        int y = 0;
    };
    

    add to list

    void GameCalculate::restartGame()
    {
        _forSend.clear();
        int label = 0;
        for(int y = 0;y < _y; ++y)
        {
            for(int x = 0;x < _x; ++x)
            {
                MinePlace *n = new MinePlace();
                n->mark = label;
                n->bomb = QRandomGenerator64::global()->bounded(0,2);
                n->x = x;
                n->y = y;
                _forSend.append(n);
                ++label;
            }
        }
    
        for(auto &a: _forSend)
        {
            qDebug() << a->mark;
        }
    }
    

    and send

    Q_INVOKABLE QList<MinePlace*> getModel(){return _forSend;}
    

    in QML

    property var gameModel: null
    //some code
        Component.onCompleted: {
            gameModel = gamecalcID.getModel();
            console.info(gamecalcID.getModel())
        }
    

    as a result I see the output of the resulting list

    qml: [MinePlace(0x1da60e95530),MinePlace(0x1da60e95560),MinePlace(0x1da60e94f60),MinePlace(0x1da60e955f0),MinePlace(0x1da60e95050),MinePlace(0x1da60e95620),MinePlace(0x1da60e95080),MinePlace(0x1da60e950b0),MinePlace(0x1da60e951d0),MinePlace(0x1da60e95650),MinePlace(0x1da60e95140),MinePlace(0x1da60e95170),MinePlace(0x1da60e95200),MinePlace(0x1da60e95590),MinePlace(0x1da60e95230),MinePlace(0x1da60e95260),MinePlace(0x1da60e95290)
    

    It’s clear that these are numbers of memory cells (which is expected, I work with pointers), but how can I get a normal data array from them in QML? And even in an understandable format?

    In theory, of course, I can make it simpler - make several properties and store several arrays in them and compare the corresponding values, but this solution seems strange and incorrect to me.

    1 Reply Last reply
    0
    • C Charby
      25 Sept 2024, 21:25

      @Anton1978 sorry my mistake, I meant :

       console.info(gameModel[0].mark) // should be 0
      console.info(gameModel[0].mark) // should be 1
      
      P Online
      P Online
      Pl45m4
      wrote on 25 Sept 2024, 22:06 last edited by
      #6

      @Charby said in How to pass an array (list) filled with custom data to QML?:

      console.info(gameModel[0].mark) // should be 0
      console.info(gameModel[0].mark) // should be 1
      

      console.info(gameModel[1].mark) // should be 1

      :)

      @Anton1978

      As @Charby wrote above, you can access the list data using the index and the member you want to address, like this:
      (with the changes shown below)

      // return a list reference
      // consider to make it const if you dont plan to modify the list later (only in C++ code)
      Q_INVOKABLE const QList<MinePlace *> &getModel() { return _forSend;}
      

      In QML something like

      property var gameModel = gamecalcID.getModel();
      for (var mp in gameModel)
              console.log(mp.mark)
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Charby
        wrote on 25 Sept 2024, 01:41 last edited by
        #2

        Out of curiosity, what is the ouput of console.info(gameModel[0].label) ?

        A 1 Reply Last reply 25 Sept 2024, 21:15
        0
        • C Charby
          25 Sept 2024, 01:41

          Out of curiosity, what is the ouput of console.info(gameModel[0].label) ?

          A Offline
          A Offline
          Anton1978
          wrote on 25 Sept 2024, 21:15 last edited by
          #3

          @Charby MinePlace(0x1da60e95530)

          C 1 Reply Last reply 25 Sept 2024, 21:25
          0
          • A Anton1978
            25 Sept 2024, 21:15

            @Charby MinePlace(0x1da60e95530)

            C Offline
            C Offline
            Charby
            wrote on 25 Sept 2024, 21:25 last edited by
            #4

            @Anton1978 sorry my mistake, I meant :

             console.info(gameModel[0].mark) // should be 0
            console.info(gameModel[0].mark) // should be 1
            
            P 1 Reply Last reply 25 Sept 2024, 22:06
            1
            • C Offline
              C Offline
              Charby
              wrote on 25 Sept 2024, 21:27 last edited by
              #5

              it seems to me that you successfully passed an array (list) filled with custom data to QML...or do you need something else ?

              1 Reply Last reply
              0
              • C Charby
                25 Sept 2024, 21:25

                @Anton1978 sorry my mistake, I meant :

                 console.info(gameModel[0].mark) // should be 0
                console.info(gameModel[0].mark) // should be 1
                
                P Online
                P Online
                Pl45m4
                wrote on 25 Sept 2024, 22:06 last edited by
                #6

                @Charby said in How to pass an array (list) filled with custom data to QML?:

                console.info(gameModel[0].mark) // should be 0
                console.info(gameModel[0].mark) // should be 1
                

                console.info(gameModel[1].mark) // should be 1

                :)

                @Anton1978

                As @Charby wrote above, you can access the list data using the index and the member you want to address, like this:
                (with the changes shown below)

                // return a list reference
                // consider to make it const if you dont plan to modify the list later (only in C++ code)
                Q_INVOKABLE const QList<MinePlace *> &getModel() { return _forSend;}
                

                In QML something like

                property var gameModel = gamecalcID.getModel();
                for (var mp in gameModel)
                        console.log(mp.mark)
                

                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                1
                • A Anton1978 has marked this topic as solved on 26 Sept 2024, 15:46

                1/6

                24 Sept 2024, 18:20

                • Login

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