Change items in a container (e.g. QList)
-
Hi.
I have a QList<MyClass> and i want to access and change the items in the list. Through google i came up withlist->operator [](index).setSomeValue();
where list is a pointer to QList.
I have two questions. Whylist[index].setSomeValue();
doesnt work? Shouldnt it be the same?
And the second: when i come to this point "list->operator [] (index)." the qcreator doesnt show me any posibility to chose something in the autocompletion, although when i just type the setSomeValue() after the point it compiles and work?Perhas there is a better way to change an item in a container?
-
Hi
is thatQList<MyClass> mylist; OR QList<MyClass *> mylist;
as that change the syntax
version 1 would be
mylist[index].setSomeValue();
and version 2 would be
mylist[index]->setSomeValue(); -
class MyClass { public: void bing() {}; MyClass() {} }; QList<MyClass>* list = new QList<MyClass>; MyClass tmp; list->append(tmp); (*list)[0].bing(); (delete list;)
Can I ask why you want the list to be a pointer to a list and not just a list?
-
@Maser
well the normal case if that its a non pointer
QList<MyClass *> list;but you get same situation if u send it as paramater
void somefunc( QList<MyClass *> * List);
somefunc(&list);
so it really depends on what you do with list and how long it must live.
(note normally it would be &) -
@Maser
hi
all QWidgets are QObjects.
QObjects have copy constructor disabled. They cannot be cloned.
So you cannot have QWidgets in a list if you define it as
QList<MyClass>
IT must be pointers.However for you own classes, there is no need for pointers.
QList<MyClass> *list = pointer to list of objects. Not possible with most QWidgets
QList<MyClass *> *list = pointer to list of pointers to objects. can contain QWidgets
QList<MyClass> list; Just a list of Myclasses. ( best option normally) -
Also, just as note.
Most QWidgets takes a parent (another qwidget) in constructor.
It means the parent now owns the child and will delete it when it dies it self.So if you have childs in a list and give them to parents,
the list should Not delete the childs as the parent will. :) -
-
@Maser
yes watching pointers is always needed :)- BTW, why cant i mark the thread solved?
You might not have asked it as question.
On first post look in topic tools button.
There is "Ask as Question"
Then you can mark it as solved :)
- BTW, why cant i mark the thread solved?