Results 1 to 7 of 7

Thread: Conversion Char Array to string

  1. #1
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Conversion Char Array to string

    Hi
    I want to show my array's on a label, so i want to convert my char array to string for a label.
    How may i do this?

  2. #2
    Join Date
    Apr 2008
    Location
    Pavlodar, Kazakhstan
    Posts
    22
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conversion Char Array to string

    Just use QLabel::setText(const QString &) function with your array as the argument. QString class will make conversion for you.

    For instance:
    Qt Code:
    1. char array[] = "Some text";
    2. QLabel label;
    3. label.setText(array);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to mitro for this useful post:

    anafor2004 (2nd May 2008)

  4. #3
    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: Conversion Char Array to string

    by the way,
    why dont u use QString itself instead of the char[] ??

  5. #4
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Conversion Char Array to string

    Hello,

    jst convert ur array[] to QString..

    Qt Code:
    1. char array[] = "Hello World";
    2. QString *str = NULL;
    3.  
    4. str = array;
    5.  
    6. QLabel tempLabel;
    7.  
    8. tempLabel.setText( str );
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Conversion Char Array to string

    it is not working,

  7. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Conversion Char Array to string

    Quote Originally Posted by anafor2004 View Post
    it is not working,
    Very very good error description...

    What does not work? What did you try to fix it? Did you even try to solve it the way the former posts suggested?

  8. #7
    Join Date
    Apr 2008
    Location
    Pavlodar, Kazakhstan
    Posts
    22
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conversion Char Array to string

    The last method is incorrect. You cannot just assign a pointer to char array to a pointer to QString. Why don't use the solution I've suggested earlier? There is no need to make things harder. If you need QString not only to set for a label but for other aims too, just instantiate QString with your array as a parameter:
    Qt Code:
    1. char array[] = "Some text";
    2. QString string(array);
    To copy to clipboard, switch view to plain text mode 
    Last edited by mitro; 6th May 2008 at 16:03.

Similar Threads

  1. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 22:18
  2. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13:12
  3. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20:49
  4. String Array
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2006, 07:53
  5. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55

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.