Conversion of std::wstring to QString throws linker error

前端 未结 7 2009
一生所求
一生所求 2020-12-17 15:25

Hi I need to convert a std::wstring to QString and I tried the most obvious way of

std::wstring wideString;
QString qtString = QStri         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 16:03

    Best solution to the problem is to set the option "Treat wchar_t was Built-in Type" to No. However in some cases this might not be possible.

    For example xerces_c is compiled with wchar_t as Built-in Type. If you need to use both xerces_c then you must either recompile QT or xerces_c to match a common Built-in Type setting.

    Windows uses UTF16 charset so does QT for unicode strings. Thus, the alternative solution below might be a life saver.

    /*! Convert a QString to an std::wstring */
    std::wstring qToStdWString(const QString &str)
    {
    #ifdef _MSC_VER
        return std::wstring((const wchar_t *)str.utf16());
    #else
        return str.toStdWString();
    #endif
    }
    
    /*! Convert an std::wstring to a QString */
    QString stdWToQString(const std::wstring &str)
    {
    #ifdef _MSC_VER
        return QString::fromUtf16((const ushort *)str.c_str());
    #else
        return QString::fromStdWString(str);
    #endif
    }
    

提交回复
热议问题