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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on 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.

    JKSHJ 1 Reply Last reply
    0
    • M maydin

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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
      • JonBJ JonB

        @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 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.

        JonBJ 1 Reply Last reply
        0
        • M maydin

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #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
          2
          • fcarneyF fcarney

            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.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on 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
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on 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.

              JKSHJ 1 Reply Last reply
              0
              • fcarneyF fcarney

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

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on 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
                • JonBJ JonB

                  @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 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.

                  JonBJ 1 Reply Last reply
                  0
                  • M maydin

                    @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.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #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
                    0
                    • JonBJ JonB

                      @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 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

                      • Login

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