How to change a member of a struct in QList
-
I have a struct that has some variables, I made a QList from this struct, how can I change a struct in a given position in List?
struct Command { QByteArray cmd; quint16 retry; bool acked; }; .... QList<Command> commandsList; .... commandsList.value(cmdIndex).acked = true; /* Error: using temporary as lvalue [-fpermissive]*/ ... commandsList.at(cmdIndex).acked = true; /*Error:assignment of member 'Command::acked' in read-only object*/ /*I know, function at() returns const & which is not changable.*/
How can I change a member in QList?
-
Hello,
You can use the array access operator.
http://doc.qt.io/qt-5/qlist.html#operator-5b-5dKind regards.
-
Thanks for answer, but it didn't work.
commandsList.operator[](cmdIndex).acked = true; // Am I using it true? (Runtime error) commandsList[cmdIndex].acked = true; //Runtime Error
I am receiving <Runtime error!>
-
Thanks for answer, but it didn't work.
commandsList.operator[](cmdIndex).acked = true; // Am I using it true? (Runtime error) commandsList[cmdIndex].acked = true; //Runtime Error
I am receiving <Runtime error!>
@kahlenberg said:
hi that is odd.
Can you show where you add items to commandsList ?
as to get runtime error then cmdIndex might be higher than
the number of items in the QList. -
Oh sorry, cmdIndex was out of range. I am sooo sory :)
-
Oh sorry, cmdIndex was out of range. I am sooo sory :)
@kahlenberg
Well that happens to all of us :)
Good it was something simple and not some nasty memory overrun some where else.