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. How to properly store objects into a list
Forum Updated to NodeBB v4.3 + New Features

How to properly store objects into a list

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 17.3k Views 1 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.
  • L Offline
    L Offline
    ludde
    wrote on last edited by
    #4

    I think the choice depends very much on what MyClass is. If it's a lightweight class that is passed around by value, then put objects in the list. If it's a more complex class, e.g. something derived from QObject, you should create the objects on the heap (using new) and store pointers in the list. Of course, then you have to make sure the objects are destroyed at some point, e.g. when the list is destroyed.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #5

      [quote author="Pufo" date="1309620091"]
      What is the difference between:
      QList<MyClass *> object
      QList<MyClass> object

      And how should the objects that i'm adding be created, on stack or on heap ?
      [/quote]

      Regarding the lists, will the second sollution create the objects on the heap while the first one will only store pointers and the objects itself can live anywhere. The content of the list will be created on the heap.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pufo
        wrote on last edited by
        #6

        My class has 3 members. Two integers and a QString pointer.

        I've created a list "QList<MyClass> *data" which is populated with objects ( not pointer to objects ).

        If i do takeFirst() method, my program crashes.
        I can avoid that if i comment out the constructor of MyClass.

        I can't understand this behavior.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          ludde
          wrote on last edited by
          #7

          I think you'll have to show us MyClass for us to be able to help you with that.
          I assume you have added at least one object to the list before you do takeFirst(), right?

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pufo
            wrote on last edited by
            #8

            I have almost 2000 objects, verified with size() method.

            @#include "myline.h"
            #include <QString>
            #include <QChar>

            static QChar badArray [] = {L'þ',L'ã',L'Ã',L'ª',L'º',L'â',L'Î',L'î', L'Þ'};
            static QChar goodArray [] = {'t','a','A','S','s','a','I','i','T'};

            MyLine::MyLine(QString &p_text, int p_startTime, int p_endTime){

            text = new QString(p_text);
            startTime = p_startTime;
            endTime = p_endTime;
            

            }

            MyLine::MyLine(){

            text = new QString("");
            startTime = 0;
            endTime = 0;
            

            }

            MyLine::MyLine(const MyLine &obj){

            text = obj.text;
            startTime = obj.startTime;
            endTime = obj.endTime;
            

            }

            void MyLine::addDelay(int mili){

            startTime += mili;
            endTime += mili;
            

            }

            void MyLine::subtractDelay(int mili){

            startTime -= mili;
            endTime -= mili;
            

            }

            void MyLine::correct(){

            for (int i = 0; i < 9; i++) {
            
                if (text->indexOf(badArray[i]) != -1){
            
                    text->replace(badArray[i], goodArray[i]);
                }
            }
            

            }

            MyLine::~MyLine(){

            delete text;
            

            }@

            1 Reply Last reply
            0
            • L Offline
              L Offline
              ludde
              wrote on last edited by
              #9

              You don't want to use QString that way...
              Just use QString, not pointer to QString. And don't create them using new.

              Edit: I guess it might work, if you copy the string in the copy constructor as well. But you really don't want to use QString this way anyway.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Pufo
                wrote on last edited by
                #10

                Can you tell my why i don't want to use pointers for QString ?

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  ludde
                  wrote on last edited by
                  #11

                  QStrings are implicitly shared. You can safely pass them around as objects without worrying about how, when and where they are copied, and how much space they are taking up. I guess you can use pointers, but it just makes things much more difficult for you.

                  What did you do to get that error message?

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    ludde
                    wrote on last edited by
                    #12

                    If you use QString instead of a pointer, you can get rid of the destructor and copy constructor.
                    Also, you should probably consider using initializer lists in your constructors, i.e.:
                    @
                    MyLine::MyLine(const QString &p_text, int p_startTime, int p_endTime)
                    : text(p_text)
                    , startTime(p_startTime)
                    , endTime(p_endTime)
                    {
                    }
                    @

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #13

                      As a side node, the crash comes from here:

                      @
                      MyLine::MyLine(const MyLine &obj){

                      text = obj.text;
                      startTime = obj.startTime;
                      endTime = obj.endTime;
                      

                      }
                      @

                      You copy the pointer. and in the destructor of both objects, you then destroy it (this and obj)

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      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