Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Converting PyQt5 (Python) to Qt C++ QtWebSockets



  • so i made a sketch / demo in PyQt5 and now im converting it to Native Qt C++

    but im lacking a good tutorial on it so i can convert my python code

    from PyQt5 import QtCore, QtWebSockets, QtNetwork
    from PyQt5.QtCore import QUrl, pyqtSignal
    
    class Socket(QtCore.QObject):
    
        error = pyqtSignal(QtNetwork.QAbstractSocket.SocketError)
        pingError = pyqtSignal(QtNetwork.QAbstractSocket.SocketError)
        connReady = pyqtSignal()
    
        def __init__(self, *args, **kwargs):
            QtCore.QObject.__init__(self)
            self.client = QtWebSockets.QWebSocket('', QtWebSockets.QWebSocketProtocol.Version13, None)
            self.client.error.connect(self.error)
            self.client.error.connect(self.pingError)
            self.client.connected.connect(self.connReady)
    
            self.hostAdr = kwargs['host']
    
        def do_ping(self):
            print('client: ping')
            self.client.ping(b'ping')
    
        def onPong(self, elapsedTime, payload):
            print('onPong - time: {} ; payload: {}'.format(elapsedTime, payload))
    
        def Connect(self):
            self.client.open(QUrl(f"ws://{self.hostAdr}:80"))
            self.client.pong.connect(self.onPong)
    
        def sendMessage(self, bName, bValue):
            self.client.sendTextMessage('{"' + bName + '": "' + str(bValue) + '"}')
    
        def sendMessage2(self, bValue):
            self.client.sendTextMessage(str(bValue))
    
        def close(self):
            self.client.close()
    

    anyone who can help me? :)



  • @Kris-Revi
    Since there isn't any answer other than just do the conversion, using docs as necessary, may I ask: if you are familiar/comfortable with Python but not with C++, why are you trying to do this conversion?



  • @JonB 2 reasons

    1. Native Qt C++ is faster
    2. Making a .exe from PyQt5 Python is a pain (many people have said to me that i should just convert it to C++)


  • @Kris-Revi Converting a code to a language is not trivial (in general) so there will not be a tutorial that indicates what you are looking for. On the other hand, I don't see any attempt from you so it discourages me from trying to help you.

    My recommendations is:

    • Learn C ++,
    • Learn Qt,
    • Check the Qt examples with websockets,
    • Try something to solve your problem, etc.

    If you can't do the above then hire someone who has already done the above and then translate your code.

    Your post seems to say: can you do my work for free?

    I don't see any specific problem.



  • @eyllanesc

    and i quote

    but im lacking a good tutorial
    


  • @Kris-Revi There is no such magic tutorial, the closest are the official examples: https://doc.qt.io/qt-5/qtwebsockets-examples.html



  • @eyllanesc i did follow the example on the docs just to test it out but im getting a shit load of errors for some reason :S i've copied JUST like it is in the example just to see it working (connecting to my ESP32)

    Image
    Errors



  • @Kris-Revi That is a linking problem, check if the Qt you have installed has the QtWebsockets module installed. Also show your .pro.



  • @eyllanesc i fixed it by adding

    QT += websockets 
    

    to the .pro

    but im getting this now

    00:00:44: Cannot retrieve debugging output.
    

Log in to reply