Important: Please read the Qt Code of Conduct -

[SOLVED]Problems transforming QString to microsofts' CString

  • Hi,
    I have a set of functions from a previous MFC application that I would like to use in my current Qt application. The problem is that i get this link error:

    widget.obj:-1: error: LNK2019: unresolved external symbol "public: static int __cdecl CConverter::Convert(class ATL::CStringT<unsigned short,class ATL::StrTraitATL<unsigned short,class ATL::ChTraitsCRT<unsigned short> > >,class ATL::CStringT<unsigned short,class ATL::StrTraitATL<unsigned short,class ATL::ChTraitsCRT<unsigned short> > >,enum FormatType)" (?Convert@CConverter@@SAHV?$CStringT@GV?$StrTraitATL@GV?$ChTraitsCRT@G@ATL@@@ATL@@@ATL@@0W4FormatType@@@Z) referenced in function "private: void __thiscall Widget::on_convertButton_clicked(void)" (?on_convertButton_clicked@Widget@@AAEXXZ)

    when I try to call this function:

    static BOOL Convert(CString strSourcePath, CString strTargetPath, FormatType formatType);

    From I saw that you can subsitute CStrings for const char * as arguments so I did my transformations and called the function like this:

    void Widget::on_convertButton_clicked()
        //1.CONVERT THE QString to a CString
        QString str1 = ui->lineEdit->text();
        const char * strSrc=str1.toUtf8().constData();
        QString destFolder("C:/Users/Daniel/Desktop/karamage.pdf");
         const char * strDest=destFolder.toUtf8().constData();
        BOOL bResult = FALSE;
        bResult = CConverter::Convert(strSrc,strDest, ConvertPPT);
          QMessageBox message;
          QMessageBox message;

    ConvertPPT is just an enum defined in my header.I also have to mention that the app links to some MFC stuff So I have a


    in my .pro file if this helps.Am I doing sth wrong or is there any linking I have missed form my app to build successfully? I would appreciate any help.
    Thank you for your time.

  • Moderators

    Yes, you're doing it wrong.

    const char * strSrc=str1.toUtf8().constData();

    toUtf8 creates a temporary QByteArray. You then take the address of the data inside it and store the pointer. But after the ; the temporary QByteArray is destroyed and you're left with a pointer to an invalid memory region.

    The other thing is that CString character is either char or wchar_t, depending on whether or not _UNICODE macro is defined in your program. So make sure which one you use and convert the QString accordingly using either toUtf8() or toWcharArray().

    Next thing is that the docs you cited says you can substitute CString for const char* because it provides conversion operators, but not the other way around, so you need to construct the CString objects first to pass it into the function.

    Apart from these coding errors the actual error message is from the linker saying it can't find the definition of your Convert method, so make sure you're including it in your project.

  • Thanks Chris for the reply:
    I have been able to compile the code with the changes below:

    //The source office file
    QString strSrc = ui->lineEdit->text();
    QByteArray srcBa1 = strSrc.toLocal8Bit();
    const char *srcString1 =;
    CString myStringSrc(srcString1);
    //The dest office file
    QString strDest="C:/Users/Daniel/Desktop/karamage.pdf";
    QByteArray destBa2 = strDest.toLocal8Bit();
    const char *destString1 =;
    CString myStringDest(destString1);


    The method was not being found after these changes though ,I remembered having dealt with such issues in the past and moved the implementation of the convert functions in the header file and it magically compiled(in my dummy test project) ,I am sure there is an explanation to this but may be beyond my understanding :-). I am having other issues integrating all that in the real project but I guess its better to put that in another thread.

  • Moderators

    Just a comment. If the string contains null character ( \0 ) the conversion you did will chop off what's behind it.
    To correct this you would need to pass the size of the array too:

    QByteArray srcBa1 = ui->lineEdit->text().toLocal8Bit();
    CString myStringSrc(, srcBa1.size());

    I'm also not sure toLocal8Bit is the right encoding. The more probable would be toLatin1(), but QString can contain unicode characters, in which case you will get the wrong results. So if you have defined _UNICODE you should rather use toWcharArray(), and if not then toUtf8().

  • so is there a perfect solution to Conver between QString and CString?
    like this ?
    QString to CString: toLocal8Bit for multibyte encoding, toWString for unicode encoding.
    CString to QString: fromLocal8Bit for multibyte encoding, fromWString for unicode encoding.

    and when to use toLatin1()?
    when to use toUtf8()?

Log in to reply