Results 1 to 5 of 5

Thread: How to copy a linklist

  1. #1
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to copy a linklist

    Hi

    I am used to creating and using linklists in c but not used to the QLinkedList class, i have one setup and populated in the mainwindow function but then need to call a function and pass a copy of this linklist into it. The problem is it seems not to keep the two lists as seperate items and if i change one the other changes.

    Is there a specific way to do this?

    thanks

    James

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to copy a linklist

    QLinkedList has a copy constructor witch initialize a new list with the shared elements of your original list and copy the list only if you modify the "copy" (copy-on-write)

  3. The following user says thank you to Zlatomir for this useful post:

    zoz (17th September 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to copy a linklist

    Thanks for your quick responce my current bit of code is
    Qt Code:
    1. ceguSubWidget::ceguSubWidget(QLinkedList<tabptr> tabs)
    2. {
    3. tablist = tabs;
    4. setAttribute(Qt::WA_DeleteOnClose);
    5. isUntitled = true;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Is that correct or incorrect.

    Thanks

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to copy a linklist

    If you use the list to store pointers to type the copy of the list doesn't copy the objects pointed by the pointers stored in the list (it copy the pointers)

    If tabptr is a type defined by you, code a copy constructor witch must do a deep copy (copy of objects the pointers points to)

    LE: i think this is your problem: you end up with two pointers (two copy, one in each copy of the list) per each object, if you modify the object by de-reference one pointer, the second pointer will point to the same modified value (not the one before modification)
    Last edited by Zlatomir; 18th August 2010 at 10:58.

  6. The following 2 users say thank you to Zlatomir for this useful post:

    parsnips146 (18th August 2010), zoz (17th September 2010)

  7. #5
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to copy a linklist

    I created a copy constuctor and it working now.

    Thanks for your help

Similar Threads

  1. Copy row(s) from QTableWidget
    By allensr in forum Qt Programming
    Replies: 10
    Last Post: 1st February 2017, 08:59
  2. Copy a log file
    By fortyhideout12 in forum Qt Programming
    Replies: 2
    Last Post: 15th August 2009, 08:05
  3. Circular Linklist.
    By AmolShinde_8 in forum Qt Programming
    Replies: 2
    Last Post: 31st October 2008, 04:23
  4. copy XML file
    By mattia in forum Newbie
    Replies: 1
    Last Post: 8th February 2008, 13:30
  5. Replies: 1
    Last Post: 23rd January 2007, 07:46

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.