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. Converting QByteArray to QString
Forum Updated to NodeBB v4.3 + New Features

Converting QByteArray to QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
qbytearrayqstringqprocess
13 Posts 5 Posters 10.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.
  • S Offline
    S Offline
    Sina Ranjkesh zade
    wrote on 17 Jul 2021, 12:47 last edited by Sina Ranjkesh zade
    #3

    This is my function:

    QByteArray PRID::dataProcess(QByteArray data, NkMetadataBag &metadata)
    {
    
        auto path = QString::fromLocal8Bit(qgetenv(NK_HOME_PATH));
    
        QProcess p;
        QStringList params;
        qDebug() << "d data in processor: " << data.length() << endl;
        qDebug() << "data in processor: " << QString::fromUtf8(data.data(),data.size()).length() << endl;
        
        params << path + "/" + _scriptName + ".py" << QString::fromUtf8(data.data(),data.size());
    
        p.start("python", params);
        p.waitForFinished(-1);
        QString p_stdout = QString::fromLocal8Bit(p.readAll());
        p_stdout = p_stdout.remove("\r\n");
        return p_stdout.toLocal8Bit();
    }
    

    As you can see, I get an input as "data" which is QByteArray. I get this result in application output:

    d data in processor: 3686400
    data in processor: 3674351
    

    But in python when I get input arguments using "sys.argv" command, it's length is 2805.
    I forgot to say that I'm working on Ubuntu 20.04.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 Jul 2021, 13:42 last edited by
      #4

      As already told you on so - make sure there is no \0 char in your string - otherwise it might get ignored either by QString::fromLocal8Bit() (which is still wrong when you use utf8 in python) or by python arguments parser.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply 17 Jul 2021, 16:05
      1
      • C Christian Ehrlicher
        17 Jul 2021, 13:42

        As already told you on so - make sure there is no \0 char in your string - otherwise it might get ignored either by QString::fromLocal8Bit() (which is still wrong when you use utf8 in python) or by python arguments parser.

        S Offline
        S Offline
        Sina Ranjkesh zade
        wrote on 17 Jul 2021, 16:05 last edited by Sina Ranjkesh zade
        #5

        @Christian-Ehrlicher
        What should I do with them?
        I searched the QString and it seems there isn't any \0.
        Do you know how can I find them?
        Each of the elements of QByteArray was a uint8 number.

        C 1 Reply Last reply 19 Jul 2021, 00:40
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Jul 2021, 17:11 last edited by
          #6

          Hi,

          Why do you want to dump a blob of binary data as argument to a Python script ?

          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
          3
          • S Sina Ranjkesh zade
            17 Jul 2021, 16:05

            @Christian-Ehrlicher
            What should I do with them?
            I searched the QString and it seems there isn't any \0.
            Do you know how can I find them?
            Each of the elements of QByteArray was a uint8 number.

            C Offline
            C Offline
            ChrisW67
            wrote on 19 Jul 2021, 00:40 last edited by
            #7

            @Sina-Ranjkesh-zade said in Converting QByteArray to QString:

            Each of the elements of QByteArray was a uint8 number.

            The first one of these 3686400 bytes in the input QByteArray that is zero will terminate the string conversion. The zero byte is in the input, not the output QString.

            What you are trying to do makes little sense. Even if you got a meaningful 3686400 character QString, and that's doubtful IMHO, I am fairly confident no operating system will accept such a long command line.

            Seems far more likely that the python script is expecting binary data in a file, and the file name as an argument.

            S 1 Reply Last reply 24 Jul 2021, 07:27
            3
            • C ChrisW67
              19 Jul 2021, 00:40

              @Sina-Ranjkesh-zade said in Converting QByteArray to QString:

              Each of the elements of QByteArray was a uint8 number.

              The first one of these 3686400 bytes in the input QByteArray that is zero will terminate the string conversion. The zero byte is in the input, not the output QString.

              What you are trying to do makes little sense. Even if you got a meaningful 3686400 character QString, and that's doubtful IMHO, I am fairly confident no operating system will accept such a long command line.

              Seems far more likely that the python script is expecting binary data in a file, and the file name as an argument.

              S Offline
              S Offline
              Sina Ranjkesh zade
              wrote on 24 Jul 2021, 07:27 last edited by Sina Ranjkesh zade
              #8

              @ChrisW67
              Thank's for your response.
              I want to pass an image from Qt to python. The image is high-resolution (1920*1080). I've converted the image data to QByteArray. To overcome the /0 problem, I convert to Base64 coding.
              As you said, when I want to send it as an argument to python code, I got this error:
              "execvp: Argument list too long"
              (When I convert to Base64, data size is 4915200).
              Do you have any suggestions on how to solve this problem?
              (I'm working with video frames and I should do that for each of the frames. So the speed is important too)
              Any help will be appreciated.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on 24 Jul 2021, 07:37 last edited by ChrisW67
                #9

                What is the python program expecting, or are you writing that also?

                Edit:
                You certainly do not want to be starting a new python process for every frame of video (assuming 25 or 30 FPS) . Start one process and feed it frames through its standard input or perhaps a shared memory block (QSharedMemory)

                S 1 Reply Last reply 24 Jul 2021, 08:11
                1
                • C ChrisW67
                  24 Jul 2021, 07:37

                  What is the python program expecting, or are you writing that also?

                  Edit:
                  You certainly do not want to be starting a new python process for every frame of video (assuming 25 or 30 FPS) . Start one process and feed it frames through its standard input or perhaps a shared memory block (QSharedMemory)

                  S Offline
                  S Offline
                  Sina Ranjkesh zade
                  wrote on 24 Jul 2021, 08:11 last edited by
                  #10

                  @ChrisW67
                  I'm writing that too.
                  For "standard input" if you mean input arguments of the process, I got this error:
                  "execvp: Argument list too long".
                  I also tried QSharedMemory too. I wrote data to memory in Qt successfully but I cannot read it from memory in python.

                  J C 2 Replies Last reply 24 Jul 2021, 08:25
                  0
                  • S Sina Ranjkesh zade
                    24 Jul 2021, 08:11

                    @ChrisW67
                    I'm writing that too.
                    For "standard input" if you mean input arguments of the process, I got this error:
                    "execvp: Argument list too long".
                    I also tried QSharedMemory too. I wrote data to memory in Qt successfully but I cannot read it from memory in python.

                    J Offline
                    J Offline
                    JonB
                    wrote on 24 Jul 2021, 08:25 last edited by
                    #11

                    @Sina-Ranjkesh-zade said in Converting QByteArray to QString:

                    "execvp: Argument list too long".

                    As @ChrisW67 observed earlier

                    Even if you got a meaningful 3686400 character QString, and that's doubtful IMHO, I am fairly confident no operating system will accept such a long command line.

                    You cannot pass this sort of size literal string or byte-blob on the command-line to a process. And as @ChrisW67 also said, you will want just one external process (if at all) and pass data to it via shared memory or sockets or similar.

                    I do not know whether a QSharedMemory object is supposed to be sharable with a non-Qt process. Though I would have thought it was. Can't say anything about what might happen from Python accessing it, check out documentation.

                    1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 24 Jul 2021, 19:02 last edited by
                      #12

                      As was already asked, can you explain what exactly does your Python script expects as input ?

                      How does it handle it ?

                      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
                      • S Sina Ranjkesh zade
                        24 Jul 2021, 08:11

                        @ChrisW67
                        I'm writing that too.
                        For "standard input" if you mean input arguments of the process, I got this error:
                        "execvp: Argument list too long".
                        I also tried QSharedMemory too. I wrote data to memory in Qt successfully but I cannot read it from memory in python.

                        C Offline
                        C Offline
                        ChrisW67
                        wrote on 25 Jul 2021, 11:59 last edited by
                        #13

                        @Sina-Ranjkesh-zade said in Converting QByteArray to QString:

                        For "standard input" if you mean input arguments of the process, I got this error:

                        No, I meant standard input. That is , the python program reads from a terminal and accepts input just as if you typed it (except it is the Qt program sending that input). The Qt program can send a command to the python program, send the data it needs, and read the result (if there is one) back on the python program's standard output. Or you can used shared memory, or a socket, or files, or do whatever the python program is doing in the Qt program...

                        1 Reply Last reply
                        0

                        12/13

                        24 Jul 2021, 19:02

                        • Login

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