+ Reply to Thread
Results 1 to 12 of 12

Thread: Char array to QString

  1. #1
    Join Date
    Sep 2011
    Posts
    45
    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,368
    Thanks
    19
    Thanked 992 Times in 903 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 
    ?
    Artistic Style Plugin (1.3.0) for Qt Creator 3.0.0: http://astyleplugin.sourceforge.net.

  3. #3
    Join Date
    Sep 2011
    Posts
    45
    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,368
    Thanks
    19
    Thanked 992 Times in 903 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?
    Artistic Style Plugin (1.3.0) for Qt Creator 3.0.0: http://astyleplugin.sourceforge.net.

  5. #5
    Join Date
    Sep 2011
    Posts
    45
    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,368
    Thanks
    19
    Thanked 992 Times in 903 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?
    Artistic Style Plugin (1.3.0) for Qt Creator 3.0.0: http://astyleplugin.sourceforge.net.

  7. #7
    Join Date
    Sep 2011
    Posts
    45
    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
    6,507
    Thanks
    13
    Thanked 1,367 Times in 1,309 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
    45
    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,368
    Thanks
    19
    Thanked 992 Times in 903 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.
    Artistic Style Plugin (1.3.0) for Qt Creator 3.0.0: http://astyleplugin.sourceforge.net.

  11. #11
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    54
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Char array to QString

    Hi,
    I use linux Ubuntu 10.04 with :
    gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
    Using Qt version 4.6.0
    and that above method
    Qt Code:
    1. QString string(text);
    To copy to clipboard, switch view to plain text mode 
    THank you

    Hi,
    I use linux Ubuntu 10.04 with :
    gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
    Using Qt version 4.6.0
    and that above method working in my computer
    Qt Code:
    1. QString string(text);
    To copy to clipboard, switch view to plain text mode 
    Thank you.

  12. #12
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    2
    Qt products
    Qt4

    Default Re: Char array to QString

    In older gcc and QT
    I have to use

    MyString.append(vect_char)

+ Reply to Thread

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