User:Ac001/PyQt Client

From FlightGear wiki
Jump to navigation Jump to search

Simple pyqt client

import sys
from PyQt4 import QtCore, QtGui, QtNetwork
 
class SocketTest(QtGui.QWidget):

   def __init__(self,  parent=None):
       QtGui.QWidget.__init__(self, parent)
       self.setWindowTitle("FlightGear - Socket Test")
       self.setMinimumWidth(500)

       layout = QtGui.QVBoxLayout(self)

       self.txtSocket = QtGui.QPlainTextEdit()
       layout.addWidget( self.txtSocket )

       self.quitButton = QtGui.QPushButton(self.tr("&Quit"))
       self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("close()"))
       layout.addWidget( self.quitButton )

       self.listeningSocket = FlightGearListener(self)
       self.connect(self.listeningSocket, QtCore.SIGNAL("socketDebug"), self.update_debug)

   def update_debug(self, txt):
       self.txtSocket.setPlainText( txt )


class FlightGearListener(QtCore.QObject):

   def __init__(self, parent):
       QtCore.QObject.__init__(self, parent)

       self.socket = QtNetwork.QUdpSocket(self)
       self.socket.bind(6789)
       self.connect(self.socket, QtCore.SIGNAL("readyRead()"), self.process_flightgear_datagrams)
       self.count = 0

   def process_flightgear_datagrams(self):
       while self.socket.hasPendingDatagrams():
           datagram, host, port = self.socket.readDatagram(self.socket.pendingDatagramSize())
           print "Recieving>>", self.count
           debug_string  = "%s\nraw=%s\n" %(self.count, datagram)

           properties = datagram.strip().split("\t")
           for prop in properties:
               debug_string += "%s\n" % prop
           self.emit(QtCore.SIGNAL("socketDebug"), debug_string)	
           self.count += 1

if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)
   widget =  SocketTest()
   widget.show()
   sys.exit(app.exec_())