Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Oct 2008

    Post char to QString?

    Dear all,

    how to convert char string in to QString?


    char name[20] = "Hello program";

    how to convert the above char to QString?

    bcoz i want to display that into my Listwidget?

  2. #2
    Registered User
    Join Date
    May 2009

    Re: char to QString?

    You could call:

    QString result = QString::fromAscii(name, 20);

  3. #3
    Super Contributor
    Join Date
    Oct 2009

    Re: char to QString?

    But better you use constructor directly
    QString result(name);

  4. #4
    Super Contributor
    Join Date
    Nov 2009
    Minnesota, USA

    Re: char to QString?

    Of course, you shouldn't say char name[20] = "Hello program", you should say char name[] = "Hello program" and let the compiler supply the length.

    Then omit the length if you use fromAscii(). The string will have been null-terminated by the compiler and so there's no need to specify length (and risk problems if you change the text in the future).

    Of course, the simplest, most straight-forward way to do what you want is to say:

    QString result = "Hello program";

    (You may have noticed that Qt can be cranky about when/how literal strings are converted to QString. This is unfortunately a "fact of life" with Qt, being, as it is, a layer on a layer on a layer. Your best advice is to learn two or three reliable constructs for converting strings and use those for most of your work, only resorting to more exotic constructs for special cases.)

  5. #5
    Registered User
    Join Date
    Jul 2010

    Re: char to QString?

    You can also use assignment operator of Qstring use to get Char name is new Qstring variable as follows
    QString & QString:perator= ( const char * str )

    for reference see the documentation of Qstring class or just follow link

Similar Threads

  1. Covert unsigned char to QString
    By jwzeng0312 in forum Qt
    Replies: 3
    Last Post: 2010-06-30, 19:57
  2. Qstring Query ?
    By damodharan in forum Qt
    Replies: 3
    Last Post: 2010-06-30, 13:19
  3. how to convet QString into int?
    By jainmonali21 in forum Qt
    Replies: 4
    Last Post: 2010-06-03, 13:26
  4. copy char * to char * error?
    By nokia_lin in forum Symbian
    Replies: 1
    Last Post: 2009-08-26, 08:21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts