Results 1 to 4 of 4

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.