Results 1 to 5 of 5

Thread: QList, copy problems

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QList, copy problems

    I am having trouble with an application that uses QLists to store pointers to objects. I create a QList of pointers to objects in a function, and return it. In the destructor of each object (which is in turn a QList of pointers to objects) I have the standard qDeleteAll(); clear(); rigmarole.
    Now, whenever I use that function (it's loading an XML file off disk) to provide a QList which I use to populate a QAbstractModelItem implementation, the program crashes with a SIGSEV pointing to the destructor of the object. This would seem to point to the returned QList going out of scope, and deleting it's container. Then when the model comes around to accessing the data that it thinks it can, the data pointed to by it's internal QList has been deleted, since it is after all an array of pointers. This behaviour isn't exhibited BTW if I comment out the qDeleteAll(); clear(); in the destructor.
    I am trying to write the classes contained in the QList to have deep copy constructor, so that when a copy does take place then the data is actually copied, and not just the pointers. I may be going about this in the wrong way, but I made a minimal program to demonstrate what I am trying to do. If you run it, you can see that the copy constructors are never called. My question is, what is neccessary to do to get the QList to actually make deep copies of the data it contains when the QList contains pointers? Or am I going about this the wrong way, and should I just store the objects by value?

    You can compile the program using CMake >= 2.4

    TIA,
    Valheru
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QList, copy problems

    If you have a list of pointers, the list copies pointer values, not objects.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList, copy problems

    My question is, what is neccessary to do to get the QList to actually make deep copies of the data it contains when the QList contains pointers?
    You might know the class or type of object the pointers are ? isnt it ? When copying the list, make new objects and copy all the member variables of that object. the memer variables i guess is the data u need to make a copy, isnt it ?

  4. #4
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList, copy problems

    Hmm, the reason for the crash turns out to be rather different. I was implementing QVariant operators for my classes as per the wiki, so that I could get rid of the QVariant::fromValue( *myObject ) calls. I also put a qDebug() statement in the copy constructor of my class. Then, when calling setData( QModelIndex idx, *myObject ), the debug messages showed that the copy constructor was being called recusively, despite being defined as MyClass::MyClass( const MyClass &other ). This was obviously calling the program to crash. It wasn't even entering the setData() function, as I had qDebug() statements in there to check. It just spins in a loop, making copies of *myObject.

    I am implementing copy constructors as I need the parents to changed when that object it copied, otherwise the QAbstractItemModels can't keep track of tree heirarchy. I am a bit lost as to why this is happening though

  5. #5
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList, copy problems

    Hi Valheru.

    I've looked at your post and your example code as I'm experiencing the same problems as you did. I your post it seems like you've solved the copy problem? I'm fighting to get my copy function to work and I've ended up doing a deep copy by implementing a clone function, like this. I'm using a QMap instead of your QList.
    Item Item::clone()
    {
    Item itm;
    QMap<int,QVariant>::const_iterator i = this->constBegin();
    while (i != this->constEnd()) {
    itm[i.key()] = i.value();
    ++i;
    }
    return itm;
    }

    If it's not too much trouble, will you share with me your solution from above?

    Thanks

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 18:55
  2. QObject and copy Constructors
    By December in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 16:14
  3. QTextEdit - copy and paste problems in X11
    By chezifresh in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2008, 19:21
  4. QVector copy constructor
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2008, 17:52
  5. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.