PDA

View Full Version : How to display individual character of a string?



cooper
15th July 2009, 04:59
hi everyone,

i want to display individual character from a string.

for example, i have a string ls:

QString ls = "iamastring";

i want to display the character one by one on the screen.
i tried to use a loop, but i get "invalid type error". here is my code:

for (int j = 0; j <= ls.length(); j ++)
ui->label_temp->setText(ls[j]);

can anyone give me a help, please?
thanks in advance.

aamer4yu
15th July 2009, 05:03
Thats because setText expects a QString argument and QString::operator[] returns QChar.

You might want to try - ui->label_temp->setText(QString(ls.at(j)));
used at() instead of [] as at() is faster and u dont really need a reference in this case.
Hope this helps

cooper
15th July 2009, 05:19
Thats because setText expects a QString argument and QString::operator[] returns QChar.

You might want to try - ui->label_temp->setText(QString(ls.at(j)));
used at() instead of [] as at() is faster and u dont really need a reference in this case.
Hope this helps

yes, it's very helpful. thanks for your quick reply.