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]*/
    ... = 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?

  • Qt Champions 2017

    You can use the array access operator.

    Kind 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!>

  • Qt Champions 2017

    @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 :)

  • Qt Champions 2017

    Well that happens to all of us :)
    Good it was something simple and not some nasty memory overrun some where else.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.