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. QList<Something*> what is the point?

QList<Something*> what is the point?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 7 Posters 5.5k Views 3 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.
  • HarbH Offline
    HarbH Offline
    Harb
    wrote on last edited by Harb
    #1

    Hi!
    Could someone explain me what is the point to declare QList<something *>? I saw it in Qt examples several times already. But as I know QList<something> is implemented in Qt as a class which holds poiner to the array of pointers of type <something>. It means that QList<something *> is an array of pointers to pointers to <something> which doesn't make a sense.
    Maybe I don't understand something?

    Joel BodenmannJ JKSHJ 2 Replies Last reply
    0
    • HarbH Harb

      Hi!
      Could someone explain me what is the point to declare QList<something *>? I saw it in Qt examples several times already. But as I know QList<something> is implemented in Qt as a class which holds poiner to the array of pointers of type <something>. It means that QList<something *> is an array of pointers to pointers to <something> which doesn't make a sense.
      Maybe I don't understand something?

      Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by Joel Bodenmann
      #2

      @Harb I am definitely no Qt guru at all but what you say sounds a bit wrong to me.
      QList<Foo> is a list holding Foo elements. QList<Foo*>* is a list holding pointers to Foo objects (or nullptr).

      QList uses templates. The thing between the < > brackets is the type which the list will hold. Hence <Foo> will be a list of the "real big / actual" Foo objects while <Foo*> will just be a list of pointers.

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      HarbH 1 Reply Last reply
      0
      • Joel BodenmannJ Joel Bodenmann

        @Harb I am definitely no Qt guru at all but what you say sounds a bit wrong to me.
        QList<Foo> is a list holding Foo elements. QList<Foo*>* is a list holding pointers to Foo objects (or nullptr).

        QList uses templates. The thing between the < > brackets is the type which the list will hold. Hence <Foo> will be a list of the "real big / actual" Foo objects while <Foo*> will just be a list of pointers.

        HarbH Offline
        HarbH Offline
        Harb
        wrote on last edited by Harb
        #3

        @Joel-Bodenmann nono, it make a sense if you use QVector<something>. Because QVector<someting> is an array of something in the heap. But QList is an array of poiners to something in the heap.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          When you see this in Qt's documentation/example, it means the the Something class is a QObject derived class which is non copyable. See QObject's documentation

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          HarbH 1 Reply Last reply
          1
          • David.GD Offline
            David.GD Offline
            David.G
            wrote on last edited by
            #5

            Qt Containers documentation covers a bit about copy constructor. (Employee example).

            Anyway, take with a grain of salt as I'm still in process of learning C++, but as far as my knowledge goes blindly creating objects to heap and putting them in a container is prone to memory leaks, thus QScopedPointer comes to play (and the rule of three). Recently I had to work with a list of pointers (QLinkedList), and it didn't pan out like I wanted to; I was over-complicating things.

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

              QList<Something*> Is usually not a good idea, except if the Something is QObject child, in that case, you are forced to store the pointer because QObject can't be stored by value in a QList.
              Rest of the time, I suggest you make your Something class a an "assignable data type", see the requirements for this. A list of Pointer is hard to manage and hard for other programmers to understand your code.


              Free Indoor Cycling Software - https://maximumtrainer.com

              1 Reply Last reply
              0
              • HarbH Harb

                Hi!
                Could someone explain me what is the point to declare QList<something *>? I saw it in Qt examples several times already. But as I know QList<something> is implemented in Qt as a class which holds poiner to the array of pointers of type <something>. It means that QList<something *> is an array of pointers to pointers to <something> which doesn't make a sense.
                Maybe I don't understand something?

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #7

                @Harb said:

                QList<something> is implemented in Qt as a class which holds poiner to the array of pointers of type <something>. It means that QList<something *> is an array of pointers to pointers to <something> which doesn't make a sense.

                The first sentence is only true if sizeof(Something) > sizeof(void*). Otherwise, QList stores it directly.

                So, QList<Something*> internally stores an array of pointers to Something. There are no double-ups.

                But anyway, the current opinions among Qt devs are that QVector is usually preferable to QList.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                2
                • SGaistS SGaist

                  Hi,

                  When you see this in Qt's documentation/example, it means the the Something class is a QObject derived class which is non copyable. See QObject's documentation

                  HarbH Offline
                  HarbH Offline
                  Harb
                  wrote on last edited by Harb
                  #8

                  @SGaist
                  Yes, it is explained in QList documentation as well. But for example yesterday I met that while studying Simple Tree Model example. In this example TreeItem class contain QList<TreeItem* > property, which is apparently a list of child items. TreeItem is not derived from QObject.
                  @JKSH
                  Yes, tnx! I found it in QList documentation. But as I said before I was perplexed, because of that QList<TreeItem* > thing. sizeof(TreeItem) > sizeof(void *) it means that QList<TreeItem> will store array of pointers to TreeItem. So, there is no point to write an asteriks in this case, because it will not give any effect at all. But anyway now it becоmes clear. Thanks!

                  JKSHJ 1 Reply Last reply
                  0
                  • HarbH Harb

                    @SGaist
                    Yes, it is explained in QList documentation as well. But for example yesterday I met that while studying Simple Tree Model example. In this example TreeItem class contain QList<TreeItem* > property, which is apparently a list of child items. TreeItem is not derived from QObject.
                    @JKSH
                    Yes, tnx! I found it in QList documentation. But as I said before I was perplexed, because of that QList<TreeItem* > thing. sizeof(TreeItem) > sizeof(void *) it means that QList<TreeItem> will store array of pointers to TreeItem. So, there is no point to write an asteriks in this case, because it will not give any effect at all. But anyway now it becоmes clear. Thanks!

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    You're welcome :)

                    @Harb said:

                    But as I said before I was perplexed, because of that QList<TreeItem* > thing. sizeof(TreeItem) > sizeof(void *) it means that QList<TreeItem> will store array of pointers to TreeItem. So, there is no point to write an asteriks in this case, because it will not give any effect at all.

                    There is an effect.

                    QList<TreeItem*> list1;
                    QList<TreeItem> list2;
                    
                    // ...
                    // Put 100000 TreeItems into list1, and another 100000 TreeItems into list2
                    // ...
                    
                    QList<TreeItem*> list1copy = list1; // This copies 100000 pointers (so you don't get any new TreeItems)
                    QList<TreeItem> list2copy = list2; // This copies 100000 TreeItems (so you get 100000 new TreeItems)
                    

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    HarbH 1 Reply Last reply
                    1
                    • JKSHJ JKSH

                      You're welcome :)

                      @Harb said:

                      But as I said before I was perplexed, because of that QList<TreeItem* > thing. sizeof(TreeItem) > sizeof(void *) it means that QList<TreeItem> will store array of pointers to TreeItem. So, there is no point to write an asteriks in this case, because it will not give any effect at all.

                      There is an effect.

                      QList<TreeItem*> list1;
                      QList<TreeItem> list2;
                      
                      // ...
                      // Put 100000 TreeItems into list1, and another 100000 TreeItems into list2
                      // ...
                      
                      QList<TreeItem*> list1copy = list1; // This copies 100000 pointers (so you don't get any new TreeItems)
                      QList<TreeItem> list2copy = list2; // This copies 100000 TreeItems (so you get 100000 new TreeItems)
                      
                      HarbH Offline
                      HarbH Offline
                      Harb
                      wrote on last edited by Harb
                      #10

                      @JKSH thanks for that great example! I didn't think about that. But I want to continue your reasoning, and add something. As I know Qt implement implicit sharing. So, in fact

                      QList<TreeItem*> list1copy = list1; // No any copies
                      QList<TreeItem> list2copy = list2; // No any copies

                      but than if we write

                      list1[0]->MethodWhichChangeTreeItem(); //Still no copies.
                      list2[0].MethodWhichChangeTreeItem(); // 100000 TreeItems copies

                      and than
                      list1[0]=NULL; //100000 pointers copies

                      Am I right?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        alex_malyu
                        wrote on last edited by
                        #11

                        Your question is really about "why we use pointers"? and "why we need container of pointers?".

                        People who claim that list of pointers is evil may provide you list of disadvantages, but forget that often there is no any alternative or alternatives are inefficient and depending on the circumstances may lead to even less readable code

                        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