Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pass Vector of Pointers to QML file
Forum Updated to NodeBB v4.3 + New Features

Pass Vector of Pointers to QML file

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 2 Posters 4.1k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #4

    if Thing is a QObject, everything is supported natively

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    mandruk1331M 1 Reply Last reply
    2
    • VRoninV VRonin

      if Thing is a QObject, everything is supported natively

      mandruk1331M Offline
      mandruk1331M Offline
      mandruk1331
      wrote on last edited by
      #5

      @VRonin I just thought about, what if i will create a class for the QVector<Thing*> and provide the functionality like getThat, getData etc. and send an instance of an object to the QML, will that work?

      VRoninV 1 Reply Last reply
      0
      • mandruk1331M mandruk1331

        @VRonin I just thought about, what if i will create a class for the QVector<Thing*> and provide the functionality like getThat, getData etc. and send an instance of an object to the QML, will that work?

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #6

        @mandruk1331 As long as that class is a QObject or Q_GADGET (declared and registered as MetaType) and you declare Q_IVOKABLE, slots and/or Q_PROPERTY it will

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        mandruk1331M 1 Reply Last reply
        2
        • VRoninV VRonin

          @mandruk1331 As long as that class is a QObject or Q_GADGET (declared and registered as MetaType) and you declare Q_IVOKABLE, slots and/or Q_PROPERTY it will

          mandruk1331M Offline
          mandruk1331M Offline
          mandruk1331
          wrote on last edited by
          #7

          @VRonin I created a class and inherited a QObject but how I can pass the data inside of the QVector<QObject*>? can you give a little example?

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #8
            class Thing{
            public:
            Thing(const QString& val = QString() )
            : m_foo(val){}
            Thing(const Thing& other)=default;
            Thing& operator=(const Thing& other)=default;
            ~Thing() =default;
            void setFoo(const QString& val){m_foo=val;}
            const QString& foo() const {return m_foo;}
            private:
            QString m_foo;
            };
            
            class ThingsWrapper : public QObject{
            Q_OBJECT
            Q_PROPERTY(int countThings READ countThings)
            Q_DISABLE_COPY(ThingsWrapper)
            public:
            explicit ThingsWrapper(QObject* parent=Q_NULLPTR)
            :QObject(parent){}
            virtual ~ThingsWrapper(){
            clearThings();
            }
            int countThings() const { return m_things.size();}
            Q_INVOKABLE QString fooAt(int index){
            if(index<0 || index>=m_things.size()) return QString();
            return m_things.at(index)->foo();
            }
            public slots:
            void addThing(const QString& foo){
            m_things.append(new Thing(foo));
            }
            void removeThing(int index){
            if(index<0 || index>=m_things.size()) return;
            delete m_things.takeAt(index);
            }
            void clearThings(){
            while(!m_things.isEmpty())
            delete m_things.takeLast();
            }
            private:
            QVector<Thing*> m_things;
            };
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            mandruk1331M 1 Reply Last reply
            1
            • VRoninV VRonin
              class Thing{
              public:
              Thing(const QString& val = QString() )
              : m_foo(val){}
              Thing(const Thing& other)=default;
              Thing& operator=(const Thing& other)=default;
              ~Thing() =default;
              void setFoo(const QString& val){m_foo=val;}
              const QString& foo() const {return m_foo;}
              private:
              QString m_foo;
              };
              
              class ThingsWrapper : public QObject{
              Q_OBJECT
              Q_PROPERTY(int countThings READ countThings)
              Q_DISABLE_COPY(ThingsWrapper)
              public:
              explicit ThingsWrapper(QObject* parent=Q_NULLPTR)
              :QObject(parent){}
              virtual ~ThingsWrapper(){
              clearThings();
              }
              int countThings() const { return m_things.size();}
              Q_INVOKABLE QString fooAt(int index){
              if(index<0 || index>=m_things.size()) return QString();
              return m_things.at(index)->foo();
              }
              public slots:
              void addThing(const QString& foo){
              m_things.append(new Thing(foo));
              }
              void removeThing(int index){
              if(index<0 || index>=m_things.size()) return;
              delete m_things.takeAt(index);
              }
              void clearThings(){
              while(!m_things.isEmpty())
              delete m_things.takeLast();
              }
              private:
              QVector<Thing*> m_things;
              };
              
              mandruk1331M Offline
              mandruk1331M Offline
              mandruk1331
              wrote on last edited by
              #9

              @VRonin at the moment I have a class which inherits from QObject, and inside of that class I have a struct with all the data (QString), and also I have created QVector<StructType*> , the problem is how I can access the data inside of the QVector<StructType*>obj, from QML. Because the inside of C++ I have to use obj.at(0)->StringData, and inside the QML file I can't use an arrow, how am I able to get the data from the Vector inside of the QML?

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #10

                do something like I did here:

                Q_INVOKABLE QString fooAt(int index){
                if(index<0 || index>=m_things.size()) return QString();
                return m_things.at(index)->foo();
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                mandruk1331M 1 Reply Last reply
                1
                • VRoninV VRonin

                  do something like I did here:

                  Q_INVOKABLE QString fooAt(int index){
                  if(index<0 || index>=m_things.size()) return QString();
                  return m_things.at(index)->foo();
                  }
                  
                  mandruk1331M Offline
                  mandruk1331M Offline
                  mandruk1331
                  wrote on last edited by mandruk1331
                  #11

                  @VRonin the index is getting set only in the function which changes the index number, but when I want to use it in a function like get ThinngData(){return thing.at(index)->Data} the index is the default value(the one which is on the start of the program), is there a way to tell the compiler not to do that?
                  And I forgot to say that I want to make a list view of the data, in that case I need to create a model?

                  VRoninV 1 Reply Last reply
                  0
                  • mandruk1331M mandruk1331

                    @VRonin the index is getting set only in the function which changes the index number, but when I want to use it in a function like get ThinngData(){return thing.at(index)->Data} the index is the default value(the one which is on the start of the program), is there a way to tell the compiler not to do that?
                    And I forgot to say that I want to make a list view of the data, in that case I need to create a model?

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #12

                    @mandruk1331 said in Pass Vector of Pointers to QML file:

                    is the default value(the one which is on the start of the program)

                    I did not get this, I'm sorry

                    @mandruk1331 said in Pass Vector of Pointers to QML file:

                    I want to make a list view of the data, in that case I need to create a model?

                    Exactly

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    mandruk1331M 2 Replies Last reply
                    1
                    • VRoninV VRonin

                      @mandruk1331 said in Pass Vector of Pointers to QML file:

                      is the default value(the one which is on the start of the program)

                      I did not get this, I'm sorry

                      @mandruk1331 said in Pass Vector of Pointers to QML file:

                      I want to make a list view of the data, in that case I need to create a model?

                      Exactly

                      mandruk1331M Offline
                      mandruk1331M Offline
                      mandruk1331
                      wrote on last edited by
                      #13
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • VRoninV VRonin

                        @mandruk1331 said in Pass Vector of Pointers to QML file:

                        is the default value(the one which is on the start of the program)

                        I did not get this, I'm sorry

                        @mandruk1331 said in Pass Vector of Pointers to QML file:

                        I want to make a list view of the data, in that case I need to create a model?

                        Exactly

                        mandruk1331M Offline
                        mandruk1331M Offline
                        mandruk1331
                        wrote on last edited by
                        #14

                        @VRonin So far I used your approach and create get functions which return the data i need, and I call them inside qml and just pass the index which I need, is that a correct approach?

                        1 Reply Last reply
                        1

                        • Login

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