How to increment a character in a QString (Qt)

1.5k views Asked by At

I want to achieve the same effect as

 std::string s = "abc";
 s[1]++;

but with a QString, however Qt doesn't allow me to do this:

QString s = "abc";
s[1]++;

After which I get a compilation error.

3

There are 3 answers

0
ravenmind On BEST ANSWER

Got around it by using:

text[i] = text[i].unicode() + 1;

Thanks for the help

0
Emil Laine On

QString::operator[] returns QCharRef which has no operator++.

You can get around this by doing something like:

s[1] = s[1].toAscii() + 1;
0
Amol Saindane On

You can also use at() method forQString

It works as below :

QString str = "abc";
QChar myChar = str.at(1) + 1

You can see QString operations in this link