Results 1 to 19 of 19

Thread: QList question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    Join Date
    Mar 2006
    Posts
    140
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 4 Times in 3 Posts

    Default Re: QList question

    Instead of
    Qt Code:
    1. private:
    2. QList<structOrder> m_Orders;
    To copy to clipboard, switch view to plain text mode 

    use
    Qt Code:
    1. private:
    2. QList<structOrder>* m_Orders;
    To copy to clipboard, switch view to plain text mode 

    The are certain key things you need to be aware of with C++.
    One very important thing is that when you call return at the end of the function, a COPY of the type you're returning is created to hold the data. This is because any variable declared inside a function is a STACK variable for the scope of that function. So if you declare a non pointer variable then return it, in your case in the call to orders(), you've essentially chained your call to .append(tmpOrder) on a copy of the variable that is lost as soon as the next line is called.

    Now by declaring a QList<structOrder>*, at the end of your function you'll be returning a COPY still, but it will be a copy of a POINTER. The actual instantiated QList is on the heap so isn't cleaned up at the end of the function, therefore copied QList*'s data you'll be appending to is valid and won't get lost in transit.

    In terms of architecture, you might want to re-consider this kind of thing anyway and use a wrapper function. A sideline of hiding the QList also is that you could schtick with your non pointer version:
    Qt Code:
    1. void ShoppingCartView::AddOrder(structOrder order)
    2. {
    3. m_Orders.append( order );
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by stevey; 8th May 2008 at 23:20.

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. Interface methods question
    By sincnarf in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2007, 10:20
  3. QList
    By dragon in forum Qt Programming
    Replies: 11
    Last Post: 9th May 2007, 20:15
  4. QList question
    By aamer4yu in forum Qt Programming
    Replies: 4
    Last Post: 2nd April 2007, 13:43
  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
  •  
Qt is a trademark of The Qt Company.