Conversion of std::wstring to QString throws linker error

前端 未结 7 2006
一生所求
一生所求 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 15:54

    boost::filesystem::path also seems to require the project setting "treat wchar as built-in type = yes", so vahapt's solution was the only one I could get to work.

    0 讨论(0)
  • 2020-12-17 16:03

    Edit your Visual Studio project settings and under C/C++ -> Language set the option Treat wchar_t was Built-in Type to No.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-17 16:04

    This even may happen if QtCore4.lib is linked correctly. Make sure that the VS option "Treat wchar_t as built in type" is turned OFF.

    0 讨论(0)
  • 2020-12-17 16:06

    Add

    QT += core
    

    to you .pro file. This results in what the other answer tell you to do. (be sure to rerun qmake)

    0 讨论(0)
  • 2020-12-17 16:07

    I was seeing a similar problem but none of these solutions worked. I eventually realised I was compiling with Visual Studio 2017 but linking to Qt compiled with Visual Studio 2013. Once I selected the right version it worked fine.

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