How to change string into QString?

前端 未结 7 607
一向
一向 2020-11-28 03:36

What is the most basic way to do it?

相关标签:
7条回答
  • 2020-11-28 04:09

    If by string you mean std::string you can do it with this method:

    QString QString::fromStdString(const std::string & str)

    std::string str = "Hello world";
    QString qstr = QString::fromStdString(str);
    

    If by string you mean Ascii encoded const char * then you can use this method:

    QString QString::fromAscii(const char * str, int size = -1)

    const char* str = "Hello world";
    QString qstr = QString::fromAscii(str);
    

    If you have const char * encoded with system encoding that can be read with QTextCodec::codecForLocale() then you should use this method:

    QString QString::fromLocal8Bit(const char * str, int size = -1)

    const char* str = "zażółć gęślą jaźń";      // latin2 source file and system encoding
    QString qstr = QString::fromLocal8Bit(str);
    

    If you have const char * that's UTF8 encoded then you'll need to use this method:

    QString QString::fromUtf8(const char * str, int size = -1)

    const char* str = read_raw("hello.txt"); // assuming hello.txt is UTF8 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const char*
    QString qstr = QString::fromUtf8(str);
    

    There's also method for const ushort * containing UTF16 encoded string:

    QString QString::fromUtf16(const ushort * unicode, int size = -1)

    const ushort* str = read_raw("hello.txt"); // assuming hello.txt is UTF16 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const ushort*
    QString qstr = QString::fromUtf16(str);
    
    0 讨论(0)
  • 2020-11-28 04:16

    Alternative way:

    std::string s = "This is an STL string";
    QString qs = QString::fromAscii(s.data(), s.size());
    

    This has the advantage of not using .c_str() which might cause the std::string to copy itself in case there is no place to add the '\0' at the end.

    0 讨论(0)
  • 2020-11-28 04:18

    Do you mean a C string, as in a char* string, or a C++ std::string object?

    Either way, you use the same constructor, as documented in the QT reference:

    • Qt QString Reference

    For a regular C string, just use the main constructor:

    char name[] = "Stack Overflow";
    QString qname(name);
    

    For a std::string, you obtain the char* to the buffer and pass that to the QString constructor:

    std::string name2("Stack Overflow");
    QString qname2(name2.c_str());
    
    0 讨论(0)
  • 2020-11-28 04:23

    I came across this question because I had a problem when following the answers, so I post my solution here.

    The above examples all show samples with strings containing only ASCII values, in which case everything works fine. However, when dealing with strings in Windows whcih can also contain other characters, like german umlauts, then these solutions don't work

    The only code that gives correct results in such cases is

    std::string s = "Übernahme";
    QString q = QString::fromLocal8Bit(s.c_str());
    

    If you don't have to deal with such strings, then the above answers will work fine.

    0 讨论(0)
  • 2020-11-28 04:26

    If compiled with STL compatibility, QString has a static method to convert a std::string to a QString:

    std::string str = "abc";
    QString qstr = QString::fromStdString(str);
    
    0 讨论(0)
  • 2020-11-28 04:26
    std::string s = "Sambuca";
    QString q = s.c_str();
    

    Warning: This won't work if the std::string contains \0s.

    0 讨论(0)
提交回复
热议问题