ravenmind
ravenmind

Reputation: 41

How to increment a character in a QString (Qt)

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.

Upvotes: 2

Views: 1577

Answers (3)

Amol Saindane
Amol Saindane

Reputation: 1598

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

Upvotes: 0

ravenmind
ravenmind

Reputation: 41

Got around it by using:

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

Thanks for the help

Upvotes: 2

Emil Laine
Emil Laine

Reputation: 42828

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

You can get around this by doing something like:

s[1] = s[1].toAscii() + 1;

Upvotes: 2

Related Questions