Important: Please read the Qt Code of Conduct -

QT and Unicode

  • Hello,
    I'am new to QT and I try to understand some differences between QT and old VS projects. One issue I have currently is to understand QT and its codepage. As I understand, QT is Unicode enabled. Because Iam using the VStudio 2008 compiler I assume it will be compatible with Unicode librarys from other companies. But it seems that this is wrong. I have a DLL that is compiled in unicode with VisualStudio. I can use this DLL very easy in a VisualStudio project. But If I link the DLL with a QT project I cannot use the DLL well.

    As sample:
    @ TCHAR chLicenseKey[35] = _T("KLJWVC-IXPJAP-35LAUI-AZ0G5A-2LB5KU");
    int32 res = ::Initialize(chLicenseKey, BS_ASPI_INTERNAL, BS_TRUE);@

    I tried to use this in QT but the DLL reports always an error. Because it reports a error code from the DLL header I assume that it is linked well. So I think it is a problem with the key and the string.

    So my question: Do QT use a special Unicode string that is not working with common Unicode DLLs from VisualStudio? Maybe a different UTF set? My QT source is saved in UTF-8 in case of multilanguage.
    Can I use DLLs with Unicode codepage from VisualStudio projects in QT or do I need to use Multibyte DLLs?
    Please give me some hints to solve this problem.


  • Lifetime Qt Champion


    Check that you wchar_t settings are the same between Qt and VS. IIRC the default for Qt build is "not" built-in type.

    On a side note it's Qt, QT is for Apple's QuickTime.

    Hope it helps

  • Hi,
    ok, sorry for the Qt issue ;)

    So if the DLL is compiled with wchar_t as build in then it do not work with Qt?


  • Qt's QString internally uses UTF-16, which is the same as the 'wchar_t' type does on the Windows platform (e.g. Visual Studio). If you want to get a QString as a "native" wchar_t (UTF-16) string, you can simply use QString::utf16(). The other way around, a new QString can be constructed from a a "native" wchar_t (UTF-16) string via QString::fromUtf16().

    However be aware that Qt's functions use the "const ushort *" type, instead of "const wchar_t *" type, so you may need a typcast. This typecast is safe, because both are 16-Bit types.

    @const wchar_t test = L"Test String";
    QString myString = QString::fromUtf6((const ushort
    ) test);
    const wchar_t out = (const wchar_t) myString::utf16();@

  • Hi,
    ok, sounds all well but I stil ldo not understand all the issues. Example. As I wrote I have the code:
    @ TCHAR chLicenseKey[35] = _T("KLJWVC-IXPJAP-35LAUI-AZ0G5A-2LB5KU");
    int32 res = ::Initialize(chLicenseKey, BS_ASPI_INTERNAL, BS_TRUE);
    so we have ::Initialize(TCHAR[35], int ,BOOL);
    Means the first string is a TCHAR[35] string. If I use a QString and cast it ot const wchar_t* I only get a compile error: cannot convert parameter 1 from 'const wchar_t *' to 'const TCHAR []'

    So it is still not the solution.


  • TCHAR is not a specific type. TCHAR is a macro, defined in <tchar.h>. It is part of Microsoft's way to support building the same code with Unicode (UTF-16) strings or with ANSI strings.

    Depending on your project configuration, TCHAR will either expand to "wchar_t" (Unicode) or to "char" (ANSI). Probably the function you are calling expects the type "wchar_t", but your project configuration is set to ANSI, i.e. TCHAR will expand to "char". The same way the _T() macro will either expand to an L-prefix (and thus create a Unicode string literal) or just do nothing at all (and thus create an ANSI string literal).

    If you explicitly want to use "wchar_t" (or "char"), then you should be using those directly rather than using the TCHAR macro. Using TCHAR is a good idea, only if you want to use the T-foobar macros all the way...

    (BTW: All those considerations are not Qt-specific or Qt-related at all)


    See also:

Log in to reply