Important: Please read the Qt Code of Conduct -

This code works well on Windows and does not work in Linux, in Linux it returns only the first character

  • maybe someone knows how to force to work on linux, I am grateful for the help.

    This simplified the code

    #include <QObject>
    #include <iostream>

    using namespace std;

    class ConLib : public QObject
    ConLib(QObject *parent = 0);
    void itemMaterial(char *material);


    extern "C" {

    void sendItemMaterial(char *material);




    void ConLib::itemMaterial(char material)
    QString te = QString::fromUtf16((ushort
    qDebug() << " material: " << te;


    void sendItemMaterial(char *material)
    qDebug() << "test C char: " << material;
    ConLib *conLib = new ConLib();


    from ctypes import *
    libs = cdll.LoadLibrary('')
    libs.sendItemMaterial( 'maerialre2')

  • Moderators

    You're explicitly casting char* to ushort* and using fromUtf16. Windows uses a UTF-16 at its core so that's ok (well, mostly at least). I'm not a Linux guy but I think UTF-16 is something exotic there and not widely used. I think UTF-8 (correct me someone?) is the prevailing encoding there, so you should change your code accordingly. All in all this all depends on what encoding are you serving the parameter in (in your case what encoding python uses on that platform I suppose?).

  • Lifetime Qt Champion


    Chris Kawa is right, modern linux distributions uses UTF-8.

  • corrected)) do not understand why I used UTF-16
    I added the function С @qDebug() << "test C char: " << material;@
    and he gave the first character (((
    python script is called with blender.
    python command print() return all text

  • if using @ QString::fromUtf8(material);@
    then Windows the same problem as on Linux return only the first character

  • Moderators

    Well yes, Windows uses UTF-16 and Linux UTF-8 so you can't just use one of these because it will break the other platform.

    I would suggest this:
    Inside your library use only QStrings. This way you won't be needing conversions everywhere. So change your ConLib::itemMaterial() to accept QString instead of char*.
    Then, on the boundary, preferably in one place, do a system dependent conversion eg.
    QString convert(const char* str) {
    #if defined(Q_OS_WIN)
    return QString::fromUtf16(static_cast<const ushort*>(material));
    #elif defined(Q_OS_LINUX)
    return QString::fromUtf8(material);
    #error Unsupported OS?

    void sendItemMaterial(char *material)
    QString str = convert(material);

    qDebug() << "test C char: " << str;
    ConLib *conLib = new ConLib();


  • I do not understand why but QString::fromUtf8(material);
    not working, but I solved the problem
    @void sendItemMaterial(wchar_t *material)
    QString te = QString::fromWCharArray(material);

    in Linux it works, in Windows check tomorrow at the office

    Thank you all for your help.

  • Lifetime Qt Champion

    wchar_t is not the same as a char, it size can change.

  • I know. But it works on Linux and Windows (Mac not tested), I do not understand why is not working char *

Log in to reply