'Change the ith element of a QStringList

I am searching solution for half hour and no progress.

Consider the list

QStringList foo = {};
for (int i=1; i<8; i++){
    foo << "0";
}

If some check box was clicked, I'd like to change the value of the list to "1".

So, for example, how to change the 3rd 0 by 1? Something like (pseudo code) foo.replace(3,"0","1").



Solution 1:[1]

Just apply the KISS principle ;)

foo[3] = "1";

Solution 2:[2]

I came across this thread with a similar issue, except the list was passed by reference so this option didn't work.

This line did work:

list->operator [](idx) = "val";

The operator function returns a modifiable instance at idx so you can call other functions of the type returned as well:

list->operator [](idx).prepend("val");

Credit to: https://blog.fearcat.in/a?ID=01500-adf1c894-647c-4d9c-9355-338be961e5df

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zim
Solution 2 Pasonis