Exchange data between Python and C++ using Qt TCP
-
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 theQDataStream
C++ types will look after correct language conversion, because aQDataStream
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.
-
@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 sameQByteArray
in both languages so (de)serialization works. -
@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 dohelp(QDataStream)
from Python prompt (after necessaryimport
s) and you will get PyQt5 docs on it.)] Google for:pyqt5 qdatastream
. -
@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.
-
@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.
-
@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.htmlWell I am trying to implement Python2-Python3-C++ communication system, I will write here if it works or not.
-
@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.
-
@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.