Results 1 to 6 of 6

Thread: list of pointers to a list of pointers. How?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default list of pointers to a list of pointers. How?

    Hi all, I definitely have a problem understanding and handling lists of pointers to lists of pointers

    Basically in my class I have defined the following members:


    Qt Code:
    1. LeftSide *page;
    2. QList<LeftSide *> *pageWidgets;
    3. QList<LeftSide *> pageWidgetsList;
    To copy to clipboard, switch view to plain text mode 

    where LeftSide is a subclass of QWidget. QList<LeftSide *> *pageWidgets is supposed to be a list of pointers to a list of pointers of LeftSide.
    And finally I have QList<LeftSide *> pageWidgetsList which is supposed to be a list of a list of pointers, these are the pointers of each pageWidgets instance.

    The purpose is to create n pages, store them in a list (pageWidgets), create other n pages, store them in another list (pageWidgets) and so on ..and then all these lists in a new list (pageWidgetsList). I am not even sure it is possible at this point. I have tried many different solution and this is the one which is seems to be working "the most": at least I have not problem in compiling, but then it application crashes when I try to append a page to a pageWidgets.

    Qt Code:
    1. page = new LeftSide(this);
    2. pageWidgets->append(page);
    To copy to clipboard, switch view to plain text mode 

    Hope is clear. I think I could go for other easier solutions but after all this time spent on this I would like to make this working, to understand.

    Thanks in advance.

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    29
    Thanked 50 Times in 47 Posts

    Default Re: list of pointers to a list of pointers. How?

    The implementation that you've done is right. I think you've to send the whole code, then only we will able to figure out, where the thing is missing.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: list of pointers to a list of pointers. How?

    QList<LeftSide *> *pageWidgets is supposed to be a list of pointers to a list of pointers of LeftSide.

    You say 'list' twice, but there is only one QList

    QList<LeftSide *> - list of pointers to LeftSide
    QList<LeftSide *>* - pointer to list of pointers to LeftSide
    QList<QList<LeftSide *>*> - list of (pointer to list of pointers to LeftSide)
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. The following user says thank you to amleto for this useful post:

    dotjan (30th July 2012)

  5. #4
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: list of pointers to a list of pointers. How?

    You say 'list' twice, but there is only one QList

    QList<LeftSide *> - list of pointers to LeftSide
    QList<LeftSide *>* - pointer to list of pointers to LeftSide
    QList<QList<LeftSide *>*> - list of (pointer to list of pointers to LeftSide)
    Right, this works. It was actually what I had in mind but could not figure out how to properly implement it... Very interesting.

    BTW, now I know it is possible and it works, I would like to understand from you experts if the way I am doing is something commonly used or not, or maybe even not suggested, or maybe a very good idea.

    Basically I am using these pointers to create as many objects as I need in the heap at will and always working on the right one just changing the assignment.
    So for example the user open a new tab and this creates a new LeftSide object. After ten tabs I will have ten LeftSide objects in the heap and these will be always available from the same pointer "page", just assigning "page" to the right one from the QList<LeftSide *>* list. Does it make sense?

    I feel I am playing with more advanced things, what do you thinK? Of course in the same way I have to pay attention to delete them as well..

    Thanks,
    Jan

  6. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: list of pointers to a list of pointers. How?

    honestly, it looks like something knows 'too much'. Why does anything need to know about all leftsides?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #6
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: list of pointers to a list of pointers. How?

    Quote Originally Posted by amleto View Post
    honestly, it looks like something knows 'too much'. Why does anything need to know about all leftsides?
    A leftside is a group of widgets (textedit, lineedit, treeview and some buttons) used to show text of files from a compressed one. User may want to open more compressed files simultaneously but in different tabs. So when a new compressed file is opened, a new leftside instance is created and this is showed in one tab. So far so good.. :-) Then wanted to give the user the possibility to create a new tab and look at a different file of the same compressed file. So a new leftside instance is created and showed in another tab.
    Moreover, I wanted to give the user the possibility to open also a new compressed files in another tab and look at the files of it possibly in different tabs. So even more leftside instances.

    An unlimited number of leftside can be created.. My solution was:
    instead of naming all them in a different way (page1, page2, page3....), I though it was much simple to always use the same pointer name, "page", and change the assigment of it "at need", before actually using it. So:

    A tab is created:
    Qt Code:
    1. page = new LeftSide(this);
    2. pageWidgets->append(page);
    3.  
    4. page->setDir;
    5. page->setModel();
    6. page->show();
    To copy to clipboard, switch view to plain text mode 

    Another tab is created:

    Qt Code:
    1. page = new LeftSide(this); // page now points to a different leftside object in the heap but the previous one is still there
    2. pageWidgets->append(page);
    3.  
    4. page->setDir;
    5. page->setModel();
    6. page->show();
    To copy to clipboard, switch view to plain text mode 

    and then when moving among all the different pages

    Qt Code:
    1. page = pageWidgets(tab->currentIndex());
    2.  
    3. page->setDir;
    4. page->setModel();
    5. page->show();
    To copy to clipboard, switch view to plain text mode 
    [/CODE]

    So to sum up, I created unlimited leftside objects in the heap and use the same pointer name for them and I change the assigments of it to always point to the right one (lists are used to keep track of all of them in memory)

    Not sure it is clear, code is actually much longer and I reported here only very few parts which hopefully make you understand.

    Thanks,
    Jan

Similar Threads

  1. Replies: 4
    Last Post: 15th June 2012, 00:33
  2. Replies: 3
    Last Post: 6th April 2012, 17:44
  3. Replies: 1
    Last Post: 23rd April 2011, 18:33
  4. List View with sections for alphabetialy sorted list
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 12th October 2010, 12:50
  5. problem with a list of pointers
    By maider in forum Qt Programming
    Replies: 6
    Last Post: 30th November 2009, 13:45

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.