Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. This code works well on Windows and does not work in Linux, in Linux it returns only the first character
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    red_spider
    wrote on last edited by
    #1

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

    This simplified the code

    *.h
    @
    #include <QObject>
    #include <iostream>

    using namespace std;

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

    };

    extern "C" {

    void sendItemMaterial(char *material);
    }

    @

    *.cpp

    @

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

    }

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

    }
    @

    main.py

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

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?).

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • R Offline
          R Offline
          red_spider
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            red_spider
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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);
              #else
              #error Unsupported OS?
              #endif
              }

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

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

              }
              @

              1 Reply Last reply
              0
              • R Offline
                R Offline
                red_spider
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    red_spider
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved