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. Exchange data between Python and C++ using Qt TCP
Forum Updated to NodeBB v4.3 + New Features

Exchange data between Python and C++ using Qt TCP

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 1.5k Views 2 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.
  • M Offline
    M Offline
    maydin
    wrote on 1 Dec 2020, 19:49 last edited by
    #1

    Is there an easy way of achieving this in Qt?

    To do this, I need to serialize message objects, send over TCP and deserialize in other end. This is easy if both ends are same (i.e. C++-C++ or Py-Py).

    I just want to know if I can do it using Qt types.

    J 1 Reply Last reply 2 Dec 2020, 15:27
    0
    • F Offline
      F Offline
      fcarney
      wrote on 1 Dec 2020, 20:01 last edited by
      #2

      Use JSON

      C++ is a perfectly valid school of magic.

      M 1 Reply Last reply 1 Dec 2020, 20:27
      1
      • F fcarney
        1 Dec 2020, 20:01

        Use JSON

        M Offline
        M Offline
        maydin
        wrote on 1 Dec 2020, 20:27 last edited by
        #3

        @fcarney I am looking for a binary solution for speed.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcarney
          wrote on 1 Dec 2020, 20:38 last edited by
          #4

          You might check out BSON then. I read up on this a while back. I am not sure what kind of support there is for python.

          C++ is a perfectly valid school of magic.

          J 1 Reply Last reply 2 Dec 2020, 22:55
          0
          • M maydin
            1 Dec 2020, 19:49

            Is there an easy way of achieving this in Qt?

            To do this, I need to serialize message objects, send over TCP and deserialize in other end. This is easy if both ends are same (i.e. C++-C++ or Py-Py).

            I just want to know if I can do it using Qt types.

            J Offline
            J Offline
            JonB
            wrote on 2 Dec 2020, 15:27 last edited by JonB 12 Feb 2020, 15:28
            #5

            @maydin

            Is there an easy way of achieving this in Qt?

            Do you mean using Qt's QDataStream at both ends? In that case, untested, but unless you/ @fcarney know better I assume the PyQt5/PySide2 wrappers to the QDataStream C++ types will look after correct language conversion, because a QDataStream has a fixed, defined format.

            If you mean raw Python, not PyQt5/PySide2, and so not QDataStream, then this becomes a purely Python question about writing/reading C++ data, and would be best asked there.

            The fact of using TCP/IP (Qt or not) is neither here nor there to the question. TCP/IP knows nothing about data types, it's just a raw byte stream. You can test just as easily by writing/reading a file.

            M 1 Reply Last reply 2 Dec 2020, 19:11
            2
            • J JonB
              2 Dec 2020, 15:27

              @maydin

              Is there an easy way of achieving this in Qt?

              Do you mean using Qt's QDataStream at both ends? In that case, untested, but unless you/ @fcarney know better I assume the PyQt5/PySide2 wrappers to the QDataStream C++ types will look after correct language conversion, because a QDataStream has a fixed, defined format.

              If you mean raw Python, not PyQt5/PySide2, and so not QDataStream, then this becomes a purely Python question about writing/reading C++ data, and would be best asked there.

              The fact of using TCP/IP (Qt or not) is neither here nor there to the question. TCP/IP knows nothing about data types, it's just a raw byte stream. You can test just as easily by writing/reading a file.

              M Offline
              M Offline
              maydin
              wrote on 2 Dec 2020, 19:11 last edited by
              #6

              @JonB said in Exchange data between Python and C++ using Qt TCP:

              Do you mean using Qt's QDataStream at both ends?

              Yes. That's why I ask here :)

              I know TCP communication, everything is about bytes. Since data types differ between languages, serializing/deserializing is not an easy task. There are protocols like CapNProto, Protobuf, msgpack to achieve this but I will not use them.

              Qt C++ has its own serialization protocol and QDataStream which makes using TCP very easy.

              But problem is PyQt doesn't have QVector, QList, QString (though it has QVariant). Also I am not sure if QDataStream generates same QByteArray in both languages so (de)serialization works.

              J 1 Reply Last reply 2 Dec 2020, 19:31
              0
              • M maydin
                2 Dec 2020, 19:11

                @JonB said in Exchange data between Python and C++ using Qt TCP:

                Do you mean using Qt's QDataStream at both ends?

                Yes. That's why I ask here :)

                I know TCP communication, everything is about bytes. Since data types differ between languages, serializing/deserializing is not an easy task. There are protocols like CapNProto, Protobuf, msgpack to achieve this but I will not use them.

                Qt C++ has its own serialization protocol and QDataStream which makes using TCP very easy.

                But problem is PyQt doesn't have QVector, QList, QString (though it has QVariant). Also I am not sure if QDataStream generates same QByteArray in both languages so (de)serialization works.

                J Offline
                J Offline
                JonB
                wrote on 2 Dec 2020, 19:31 last edited by JonB 12 Feb 2020, 20:55
                #7

                @maydin
                You will be able to do this from PyQt5. I can't find how yet for PyQt5, but |I see PyQt4 had https://www.riverbankcomputing.com/static/Docs/PyQt4/qdatastream.html, so there will be dedicated methods. [EDIT From PyQt5 they don't do Python docs pages any longer. But the methods will be there like in the PyQt4 docs. IIRC you can also do help(QDataStream) from Python prompt (after necessary imports) and you will get PyQt5 docs on it.)] Google for: pyqt5 qdatastream.

                M 1 Reply Last reply 3 Dec 2020, 09:53
                2
                • F fcarney
                  1 Dec 2020, 20:38

                  You might check out BSON then. I read up on this a while back. I am not sure what kind of support there is for python.

                  J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 2 Dec 2020, 22:55 last edited by
                  #8

                  @fcarney said in Exchange data between Python and C++ using Qt TCP:

                  You might check out BSON then. I read up on this a while back. I am not sure what kind of support there is for python.

                  I would suggest CBOR instead. Qt 5 has built-in support for it, and you can even stream data directly to/from the QTcpSocket:

                  • https://doc.qt.io/qt-5/qtcborcommon.html
                  • https://doc.qt.io/qt-5//qcborstreamwriter.html
                  • https://doc.qt.io/qt-5//qcborstreamreader.html

                  On the Python side, there are packages like https://pypi.org/project/cbor2/ that can handle CBOR-encoded data.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  2
                  • F Offline
                    F Offline
                    fcarney
                    wrote on 2 Dec 2020, 23:00 last edited by
                    #9

                    Yeah, I thought I saw that earlier. Could not remember the name! I could have sworn it was BSON.

                    C++ is a perfectly valid school of magic.

                    J 1 Reply Last reply 3 Dec 2020, 01:50
                    0
                    • F fcarney
                      2 Dec 2020, 23:00

                      Yeah, I thought I saw that earlier. Could not remember the name! I could have sworn it was BSON.

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 3 Dec 2020, 01:50 last edited by
                      #10

                      @fcarney said in Exchange data between Python and C++ using Qt TCP:

                      Could not remember the name! I could have sworn it was BSON.

                      They do have lots of similarities :) Both BSON and CBOR were heavily influenced by JSON.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • J JonB
                        2 Dec 2020, 19:31

                        @maydin
                        You will be able to do this from PyQt5. I can't find how yet for PyQt5, but |I see PyQt4 had https://www.riverbankcomputing.com/static/Docs/PyQt4/qdatastream.html, so there will be dedicated methods. [EDIT From PyQt5 they don't do Python docs pages any longer. But the methods will be there like in the PyQt4 docs. IIRC you can also do help(QDataStream) from Python prompt (after necessary imports) and you will get PyQt5 docs on it.)] Google for: pyqt5 qdatastream.

                        M Offline
                        M Offline
                        maydin
                        wrote on 3 Dec 2020, 09:53 last edited by
                        #11

                        @JonB said in Exchange data between Python and C++ using Qt TCP:

                        From PyQt5 they don't do Python docs pages any longer.

                        Well I have found this link. Are you sure?
                        https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtcore/qdatastream.html

                        Well I am trying to implement Python2-Python3-C++ communication system, I will write here if it works or not.

                        J 1 Reply Last reply 3 Dec 2020, 10:02
                        0
                        • M maydin
                          3 Dec 2020, 09:53

                          @JonB said in Exchange data between Python and C++ using Qt TCP:

                          From PyQt5 they don't do Python docs pages any longer.

                          Well I have found this link. Are you sure?
                          https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtcore/qdatastream.html

                          Well I am trying to implement Python2-Python3-C++ communication system, I will write here if it works or not.

                          J Offline
                          J Offline
                          JonB
                          wrote on 3 Dec 2020, 10:02 last edited by JonB 12 Mar 2020, 10:06
                          #12

                          @maydin said in Exchange data between Python and C++ using Qt TCP:

                          Well I have found this link. Are you sure?

                          Well, you have done well, that looks like the right thing. Don't know how you managed to find it, I'm pretty good a Googling, I can't see it from pyqt5 qdatastream nor even from e.g. site:www.riverbankcomputing.com pyqt5 qdatastream, only the PyQt4 one :)

                          Well I am trying to implement Python2-Python3-C++ communication system

                          BTW, since (I believe) you said you are PyQt5, it does not support Python2 officially. You may Google to try to make it work, or maybe you don't really mean support Python2.

                          M 1 Reply Last reply 3 Dec 2020, 13:34
                          0
                          • J JonB
                            3 Dec 2020, 10:02

                            @maydin said in Exchange data between Python and C++ using Qt TCP:

                            Well I have found this link. Are you sure?

                            Well, you have done well, that looks like the right thing. Don't know how you managed to find it, I'm pretty good a Googling, I can't see it from pyqt5 qdatastream nor even from e.g. site:www.riverbankcomputing.com pyqt5 qdatastream, only the PyQt4 one :)

                            Well I am trying to implement Python2-Python3-C++ communication system

                            BTW, since (I believe) you said you are PyQt5, it does not support Python2 officially. You may Google to try to make it work, or maybe you don't really mean support Python2.

                            M Offline
                            M Offline
                            maydin
                            wrote on 3 Dec 2020, 13:34 last edited by
                            #13

                            @JonB Well it doesn't support latest Qt versions but it still works in Python 2.7.

                            Currenty we are using PyQt5, supporting both Python 2 and 3 at the same time. Python 2, Qt version is 5.9 while Python 3 one is the latest. I started developing my application using PyQt4 and Python 2. Then I switched it to PyQt5, and ported to Python 3.

                            Qt didn't create problems while porting actually. Most of problems caused by Python2/3 differences. One big problem related to Qt might be string/unicode difference in Python 2. For example Qt translate method returns unicode string in Python 2. You have to be aware that to use return values in other modules like email.

                            1 Reply Last reply
                            0

                            1/13

                            1 Dec 2020, 19:49

                            • Login

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