| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 4 | |
I have a program that needs to have lists of objects shared around to
various different GUI classes. They have to be the *same* objects, i.e.
what actually needs to be shared is the pointers to said objects. I'm using
QList to do this; as far as I can tell, passing the QList by reference does
the trick. However, one of the classes needs to filter that list for
certain properties to get a new list - it has a lot of code like this:
QList<MyClass> filteredList;
for (int x = 0; x < originalList.size (); x++)
if (originalList[x].hasProperty ())
filteredList.append (originalList[x]);
This seems to be making a *copy* of the element rather than appending the
same element at the same place in memory to the new list. Is there any way
to tell append () that it needs to use the same object?
Ellen Kestrel schrieb: > I have a program that needs to have lists of objects shared around to > various different GUI classes. They have to be the *same* objects, i.e. > what actually needs to be shared is the pointers to said objects. I'm using > QList to do this; as far as I can tell, passing the QList by reference does > the trick. However, one of the classes needs to filter that list for > certain properties to get a new list - it has a lot of code like this: > > QList<MyClass> filteredList; > > for (int x = 0; x < originalList.size (); x++) > if (originalList[x].hasProperty ()) > filteredList.append (originalList[x]); > > This seems to be making a *copy* of the element rather than appending the > same element at the same place in memory to the new list. Is there any way > to tell append () that it needs to use the same object? > How should this work? Use pointers instead of objects. Christian -- [ signature omitted ]
Ellen Kestrel wrote: > This seems to be making a *copy* of the element rather than appending > the same element at the same place in memory to the new list. Is > there any way to tell append () that it needs to use the same object? Your filtered list should probably contain either pointers or references. --Dave -- [ signature omitted ]
Hi, On Friday, 18. April 2008 19:06:31 Dave Smith wrote: [snip] > Your filtered list should probably contain either pointers or references. Hmmh, I haven't certainly not delved into the C++ depth, so I am wondering how this should work with references? That would be bad, because then you would run into the risk of "dangling references" if your (local) variables went out of scope. Of course you can create references to local variables (e.g. from functon calls), but than you may open pandora's box :-) At least QList<OBJECTTYPE &> does not compile... Best Regards, Andre -- [ signature omitted ]
Hi Ellen, On Friday, 18. April 2008 18:31:44 Ellen Kestrel wrote: > I have a program that needs to have lists of objects shared around to > various different GUI classes. They have to be the *same* objects, i.e. > what actually needs to be shared is the pointers to said objects. I'm > using QList to do this; as far as I can tell, passing the QList by > reference does the trick. But this does the trick only because your are using the *same* *list* in different places. A reference to a list is not the same as a "list of references", which you probably implicitly thought of. Since you cannot create a list of references, you may think about using a list of pointers. Since two QList may only *implicitly* shared, they do a copy-on-write if you change some data in either list. It's not the same as lists in python. > However, one of the classes needs to filter that > list for certain properties to get a new list - it has a lot of code like > this: > > QList<MyClass> filteredList; > > for (int x = 0; x < originalList.size (); x++) > if (originalList[x].hasProperty ()) > filteredList.append (originalList[x]); > > This seems to be making a *copy* of the element rather than appending the > same element at the same place in memory to the new list. > Is there any way > to tell append () that it needs to use the same object? I don't hope so ;-) Use pointers, shared_pointers or something similiar, if you want to assure to keep the same *objects* ( not the same *list* of objects). HTH, Andre -- [ signature omitted ]
Well, declaring a QList<*MyClass> does not compile. I understand that passing a reference to the list passes the same list (and not a list of references), but since it states in the documentation that QList is implemented as an array of pointers, I thought there might be a way without making my own array of pointers or something like that.
Am Freitag, 18. April 2008 schrieb Ellen Kestrel: > Well, declaring a QList<*MyClass> does not compile. Try valid c++: QList <MyClass*> list; Or even better: QList <QPointer<MyClass> > list; Arnold -- [ signature omitted ]
Attachment:
signature.asc
Description: This is a digitally signed message part.
On 4/18/08, Arnold Krille <arnold@xxxxxxxxxxxxx> wrote: > > Am Freitag, 18. April 2008 schrieb Ellen Kestrel: > > Well, declaring a QList<*MyClass> does not compile. > > Try valid c++: > QList <MyClass*> list; Yeah, that's what I meant to put there. :-? It was QList<MyClass*> when it wouldn't compile, anyway. > Or even better: > QList <QPointer<MyClass> > list; I didn't know about that class - I'll try that. Thanks!