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.
  • M Offline
    M Offline
    mandruk1331
    wrote on 17 Jan 2017, 15:36 last edited by mandruk1331
    #1

    My goal is to pass a vector of pointers to a QML file, where I will be able to get the data(String) from it.

    Q_PROPERTY(QVector<Thing*> getThing READ getThing)
    and there's also a set and get functions

    main.cpp
    engine.rootContext()->setContextProperty("ThingOBJ",&ThinOBJ);

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 17 Jan 2017, 15:59 last edited by
      #2

      Use QVariantList instead of QVector<Thing*>

      "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

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mandruk1331
        wrote on 17 Jan 2017, 16:06 last edited by
        #3

        But how a QVariantList will store pointers, not the same as other containers?

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 17 Jan 2017, 17:12 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

          M 1 Reply Last reply 17 Jan 2017, 20:29
          2
          • V VRonin
            17 Jan 2017, 17:12

            if Thing is a QObject, everything is supported natively

            M Offline
            M Offline
            mandruk1331
            wrote on 17 Jan 2017, 20:29 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?

            V 1 Reply Last reply 18 Jan 2017, 08:06
            0
            • M mandruk1331
              17 Jan 2017, 20:29

              @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?

              V Offline
              V Offline
              VRonin
              wrote on 18 Jan 2017, 08:06 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

              M 1 Reply Last reply 18 Jan 2017, 08:43
              2
              • V VRonin
                18 Jan 2017, 08:06

                @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

                M Offline
                M Offline
                mandruk1331
                wrote on 18 Jan 2017, 08:43 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
                • V Offline
                  V Offline
                  VRonin
                  wrote on 18 Jan 2017, 09:00 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

                  M 1 Reply Last reply 18 Jan 2017, 09:20
                  1
                  • V VRonin
                    18 Jan 2017, 09:00
                    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;
                    };
                    
                    M Offline
                    M Offline
                    mandruk1331
                    wrote on 18 Jan 2017, 09:20 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
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 18 Jan 2017, 09:26 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

                      M 1 Reply Last reply 18 Jan 2017, 11:37
                      1
                      • V VRonin
                        18 Jan 2017, 09:26

                        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();
                        }
                        
                        M Offline
                        M Offline
                        mandruk1331
                        wrote on 18 Jan 2017, 11:37 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?

                        V 1 Reply Last reply 18 Jan 2017, 11:45
                        0
                        • M mandruk1331
                          18 Jan 2017, 11:37

                          @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?

                          V Offline
                          V Offline
                          VRonin
                          wrote on 18 Jan 2017, 11:45 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

                          M 2 Replies Last reply 18 Jan 2017, 11:53
                          1
                          • V VRonin
                            18 Jan 2017, 11:45

                            @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

                            M Offline
                            M Offline
                            mandruk1331
                            wrote on 18 Jan 2017, 11:53 last edited by
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • V VRonin
                              18 Jan 2017, 11:45

                              @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

                              M Offline
                              M Offline
                              mandruk1331
                              wrote on 18 Jan 2017, 12:23 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

                              1/14

                              17 Jan 2017, 15:36

                              • Login

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