If I append an item to a QList can I still change it?
-
In the following code, when I call
s.foo = 7;
, will this modify the value in the list? I'd like to be able to allocate an object on the list and then be able to manipulate it via a reference, but am not sure of the most efficient way to go about this.struct MyObj { int foo = 0; } void testListAdd() { MyStruct s; QList<MyStruct> list; list.append(s); s.foo = 7; }
-
Hi,
In this case, since you have 1 element in your list:
list[0].foo = 7;
-
I'm not sure I'm following you, do you mean:
list.last().foo = 7;
?
-
Can you show what you have in mind with some code.
list.last
will return a reference to the last item of the list, so if you call append and then last, you'll get the latest inserted element in the list. -
What if I wanted to say 'append a new element and set the new last element'.
As I understand it, that will set the last element currently in the list. I want to append a new element and then set that.The code you showed already does just this. Since the signature is
void QList::append(const T &value)
, what it appends is a reference, so changing the original item once it is in the list does change what is in the list. Is that what you were asking?