Results 1 to 12 of 12

Thread: Char array to QString

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Char array to QString

    I've a char array and i want to convert to QString.

    Is there a way to do this ?

    char text[24]

    to Qstring ???

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Char array to QString

    What about
    Qt Code:
    1. QString string(text);
    To copy to clipboard, switch view to plain text mode 
    ?

  3. #3
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: Char array to QString

    Infact i try

    QString MYstring(text);

    But compliler :
    no match for call to '(QString) (char [24])'


    ...Any help ??

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Char array to QString

    Qt Code:
    1. char text[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
    2. QString string(text);
    3. qWarning() << text << string;
    To copy to clipboard, switch view to plain text mode 
    works fine for me. How does your code look like?

  5. #5
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: Char array to QString

    Ok this is my code

    struct person {

    char name[24];
    char surname[24];
    };

    person P;

    strcpy (P.name,"TESTNAME");

    QString MYstring(P.name);

    The compiler gives the error above...

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Char array to QString

    Hm, builds fine with my
    i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
    . Which compiler do you use?

  7. #7
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: Char array to QString

    gcc version 4.1.2 under Linux...without any problems....

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Char array to QString

    If QT_NO_CAST_FROM_ASCII is defined then the conversion constructor from const char* is made private and the compiler would generate an error (similar but not identical to this though). If that is the case, use QString::fromAscii(), QString::fromLatin1(), or QString::fromLocal8Bit() as appropriate.

  9. #9
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: Char array to QString

    I've not defined any QT_NO_CAST_FROM_ASCII....
    i used QT 4.2.3...on linux...

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Char array to QString

    Edit: deleted, because my answer was rubbish. Haven't thought it through...
    Last edited by Lykurg; 13th December 2011 at 09:59.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 21:48
  2. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 09:28
  3. converiosn of hex data char array to Qstring
    By manoagp in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2010, 10:04
  4. Converting QString to char array
    By srohit24 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 19:19
  5. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 23:18

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.