0

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").

Sigur
  • 355
  • 8
  • 19
  • 2
    What about `foo[3] = "1";`? – Zim Jul 23 '18 at 14:45
  • @Zim, thanks so much. It is so simple solution. Why didn't I guess it? Why people don't refer to this on other posts? They talk about `replace` and others... also this should be in documentation: http://doc.qt.io/qt-5/qstringlist.html – Sigur Jul 23 '18 at 15:02
  • If you wish, consider posting and answer and I'll be glad to accept it. – Sigur Jul 23 '18 at 15:03

2 Answers2

5

Just apply the KISS principle ;)

foo[3] = "1";
Zim
  • 1,457
  • 1
  • 10
  • 21
  • 1
    I cannot find a SINGLE piece of Qt documentation that says this is how you actually do it – Dean P Feb 24 '19 at 12:38
0

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

Pasonis
  • 1
  • 1