Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change a member of a struct in QList

How to change a member of a struct in QList

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kahlenbergK Offline
    kahlenbergK Offline
    kahlenberg
    wrote on last edited by kahlenberg
    #1

    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?

    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      Hello,
      You can use the array access operator.
      http://doc.qt.io/qt-5/qlist.html#operator-5b-5d

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • kahlenbergK Offline
        kahlenbergK Offline
        kahlenberg
        wrote on last edited by
        #3

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

        mrjjM 1 Reply Last reply
        0
        • kahlenbergK kahlenberg

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • kahlenbergK Offline
            kahlenbergK Offline
            kahlenberg
            wrote on last edited by
            #5

            Oh sorry, cmdIndex was out of range. I am sooo sory :)

            mrjjM 1 Reply Last reply
            0
            • kahlenbergK kahlenberg

              Oh sorry, cmdIndex was out of range. I am sooo sory :)

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved