Results 1 to 3 of 3

Thread: How to display individual character of a string?

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to display individual character of a string?

    hi everyone,

    i want to display individual character from a string.

    for example, i have a string ls:
    Qt Code:
    1. QString ls = "iamastring";
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. for (int j = 0; j <= ls.length(); j ++)
    2. ui->label_temp->setText(ls[j]);
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display individual character of a string?

    Thats because setText expects a QString argument and QString:perator[] 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

  3. #3
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: How to display individual character of a string? [Solved]

    Quote Originally Posted by aamer4yu View Post
    Thats because setText expects a QString argument and QString:perator[] 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.

Similar Threads

  1. Determine if a font can display a character
    By jrideout in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2006, 10:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.