Results 1 to 4 of 4

Thread: Copying an QList into a new, sorted QList.

  1. #1
    Join Date
    Nov 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Post Copying an QList into a new, sorted QList.

    I wish to have a function return a sorted version of a QList. (The QList is of a custom-type called an "arblip")

    I have two ideas how to do this;

    a) Either I could copy the whole array, and use qSort, returning the copy.
    or
    b) Iterate though the items in one list, copying them into a new list.

    I'm not sure which is the better approach, and having tried both, I can get neither to work. I suspect its because I'm a neewbie and not understanding how to clone an array, or am getting my pointers mixed up, or both.

    My attempt at using qsort was basicaly;

    Qt Code:
    1. bool compareNames(arblip& s1,arblip& s2)
    2. {
    3. QDateTime temp1=s1.getLastUpdateTime();
    4. QDateTime temp2=s2.getLastUpdateTime();
    5.  
    6. return (temp1<temp2);
    7. }
    8.  
    9. QList<arblip> arbliparray::getallblips_orderedbytimestamp(){
    10.  
    11. //create temp list;
    12. QList <arblip> orderedarray;
    13.  
    14. orderedarray=arblipStore;
    15. qSort(orderedarray.begin(),orderedarray.end(),compareNames);
    16.  
    17. return orderedarray;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Which compiled, but once that part of the code was triggered, exited with a "1073741819" code.
    I suspected its probably that = statement.

    I had a similar issue with trying method b, this time crashing when I tried to assign a arblip object from one list to another.
    Qt Code:
    1. ...arblip currentblip = *arblip_iterator;
    2. orderedarray.append(currentblip); ...
    To copy to clipboard, switch view to plain text mode 

    I think my problem is not knowing how to make a complete copy of an object, rather then merely creating a new pointer to it?
    I want to return a ordered copy of the list, and for the original list to remain private and untouched.
    Last edited by Thomas Wrobel; 11th January 2010 at 17:22.

  2. #2
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copying an QList into a new, sorted QList.

    the = should be fine here
    I think the problem lies somewhere else
    but you should paste more code to see...
    I suggest you use qDebug to confirm which statement has caused the crash exactly first
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copying an QList into a new, sorted QList.

    Is there a reason you can't use the tried and tested STL sort? No copy needed, as it'll work on the original, unless you want the copy, of course.

  4. #4
    Join Date
    Nov 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copying an QList into a new, sorted QList.

    Well, in this case I do want a copy, but I'll bare that in mind in future.

    Quote Originally Posted by bood View Post
    the = should be fine here
    I think the problem lies somewhere else
    but you should paste more code to see...
    I suggest you use qDebug to confirm which statement has caused the crash exactly first
    Thanks! I was too busy getting confused with the aspects of the language alien to me, I missed a very stupid mistake earlier.
    I was calling the function twice from within a for statement (one for a constBegin, the other for a constEnd). It was no wonder it was crashing.
    Simply assigning it to use one copy and it all works.

Similar Threads

  1. Copying QList
    By frenk_castle in forum Newbie
    Replies: 3
    Last Post: 27th November 2009, 00:48
  2. QList inside a QList
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 1st July 2009, 11:59
  3. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  4. QList<QList > question
    By SubV in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 15:14
  5. QList
    By dragon in forum Qt Programming
    Replies: 11
    Last Post: 9th May 2007, 20:15

Tags for this Thread

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.