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. Creating and Maintaining dynamic array of QLists - How to?
QtWS25 Last Chance

Creating and Maintaining dynamic array of QLists - How to?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 837 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.
  • darthanaD Offline
    darthanaD Offline
    darthana
    wrote on last edited by darthana
    #1

    Need to maintain multiple QLists. QLists to be created based on need and the pointers to the QLists are to be maintained as a dynamic array. So that an QList element can be pushed or popped based on need.

    Is it possible to maintain a dynamic array of QList?
    QList<MyClass*> * m_pMyClass;
    QVector<m_pMyClass> MyClassArray;

    So based on need, create a m_pMyClass = new QList<MyClass*>; and push it to MyClassArray?

    Note: Just a beginner. Not so good with syntax

    jsulmJ JonBJ 2 Replies Last reply
    0
    • darthanaD darthana

      Need to maintain multiple QLists. QLists to be created based on need and the pointers to the QLists are to be maintained as a dynamic array. So that an QList element can be pushed or popped based on need.

      Is it possible to maintain a dynamic array of QList?
      QList<MyClass*> * m_pMyClass;
      QVector<m_pMyClass> MyClassArray;

      So based on need, create a m_pMyClass = new QList<MyClass*>; and push it to MyClassArray?

      Note: Just a beginner. Not so good with syntax

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @darthana said in Creating and Maintaining dynamic array of QLists - How to?:

      Is it possible to maintain a dynamic array of QList?

      Yes, it is.
      "QVector<m_pMyClass> MyClassArray;" - this is invalid C++ because m_pMyClass is a variable, not a type.

      What you need is:

      QVector<QList<MyClass*>> MyClassArray;
      

      To add a new QList to MyClassArray:

      MyClassArray.append(new QList<MyClass*>());
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      darthanaD 1 Reply Last reply
      2
      • darthanaD darthana

        Need to maintain multiple QLists. QLists to be created based on need and the pointers to the QLists are to be maintained as a dynamic array. So that an QList element can be pushed or popped based on need.

        Is it possible to maintain a dynamic array of QList?
        QList<MyClass*> * m_pMyClass;
        QVector<m_pMyClass> MyClassArray;

        So based on need, create a m_pMyClass = new QList<MyClass*>; and push it to MyClassArray?

        Note: Just a beginner. Not so good with syntax

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @darthana
        Just so you know, you can also maintain a list of lists rather than an array/vector if you wish:

        QList<QList<MyClass*>> MyClassList;
        

        There is not much difference between a QList and a QVector, especially if you are at Qt6.

        JoeCFDJ 1 Reply Last reply
        1
        • jsulmJ jsulm

          @darthana said in Creating and Maintaining dynamic array of QLists - How to?:

          Is it possible to maintain a dynamic array of QList?

          Yes, it is.
          "QVector<m_pMyClass> MyClassArray;" - this is invalid C++ because m_pMyClass is a variable, not a type.

          What you need is:

          QVector<QList<MyClass*>> MyClassArray;
          

          To add a new QList to MyClassArray:

          MyClassArray.append(new QList<MyClass*>());
          
          darthanaD Offline
          darthanaD Offline
          darthana
          wrote on last edited by darthana
          #4

          @jsulm @JonB Thanks!!! Will update you on my progress :)

          1 Reply Last reply
          0
          • darthanaD Offline
            darthanaD Offline
            darthana
            wrote on last edited by
            #5

            @jsulm @JonB

            I have created a QList<QList<MyClass*>> MyClassList;
            Just struggling to keep going.

            Consider I need to create multiple instances of MyClass and store it in a QList.
            Also need to create multiple QLists mentioned above and store them in MyClassList.

            How can I access a particular instance of MyClass from MyClassList? I understand a selection of QList inside MyClassList has to be done but not sure on syntax

            M 1 Reply Last reply
            0
            • darthanaD darthana

              @jsulm @JonB

              I have created a QList<QList<MyClass*>> MyClassList;
              Just struggling to keep going.

              Consider I need to create multiple instances of MyClass and store it in a QList.
              Also need to create multiple QLists mentioned above and store them in MyClassList.

              How can I access a particular instance of MyClass from MyClassList? I understand a selection of QList inside MyClassList has to be done but not sure on syntax

              M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              @darthana
              See my post HERE

              1 Reply Last reply
              0
              • JonBJ JonB

                @darthana
                Just so you know, you can also maintain a list of lists rather than an array/vector if you wish:

                QList<QList<MyClass*>> MyClassList;
                

                There is not much difference between a QList and a QVector, especially if you are at Qt6.

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @JonB Just noticed that QVector simply inherits QList in Qt6 and does not have a single extra definition.
                https://doc.qt.io/qt-6/qvector.html#details
                std::list and std::vector have quite some differences.

                JonBJ 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @JonB Just noticed that QVector simply inherits QList in Qt6 and does not have a single extra definition.
                  https://doc.qt.io/qt-6/qvector.html#details
                  std::list and std::vector have quite some differences.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #8

                  @JoeCFD said in Creating and Maintaining dynamic array of QLists - How to?:

                  @JonB Just noticed that QVector simply inherits QList in Qt6 and does not have a single extra definition.

                  That's why I wrote:

                  There is not much difference between a QList and a QVector, especially if you are at Qt6.

                  They essentially made them the same at Qt6.

                  JoeCFDJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @JoeCFD said in Creating and Maintaining dynamic array of QLists - How to?:

                    @JonB Just noticed that QVector simply inherits QList in Qt6 and does not have a single extra definition.

                    That's why I wrote:

                    There is not much difference between a QList and a QVector, especially if you are at Qt6.

                    They essentially made them the same at Qt6.

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

                    @JonB True. That is kind of confusing. I prefer to use std::vector and std::list.

                    JonBJ 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @JonB True. That is kind of confusing. I prefer to use std::vector and std::list.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #10

                      @JoeCFD
                      Fine and up to you, but then you/beginners will have to do conversions between std:: lists/vectors and Qt ones.

                      1 Reply Last reply
                      0
                      • darthanaD darthana has marked this topic as solved on

                      • Login

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