PDA

View Full Version : list of pointers to a list of pointers. How?



dotjan
28th July 2012, 12:06
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:



LeftSide *page;
QList<LeftSide *> *pageWidgets;
QList<LeftSide *> pageWidgetsList;

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.


page = new LeftSide(this);
pageWidgets->append(page);

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.

sonulohani
28th July 2012, 12:36
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.

amleto
28th July 2012, 13:05
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)

dotjan
30th July 2012, 13:13
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

amleto
30th July 2012, 17:35
honestly, it looks like something knows 'too much'. Why does anything need to know about all leftsides?

dotjan
30th July 2012, 20:07
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:


page = new LeftSide(this);
pageWidgets->append(page);

page->setDir;
page->setModel();
page->show();

Another tab is created:


page = new LeftSide(this); // page now points to a different leftside object in the heap but the previous one is still there
pageWidgets->append(page);

page->setDir;
page->setModel();
page->show();

and then when moving among all the different pages


page = pageWidgets(tab->currentIndex());

page->setDir;
page->setModel();
page->show();[/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