diff options
author | Matti Airas <matti.p.airas@nokia.com> | 2010-09-07 15:11:12 +0300 |
---|---|---|
committer | Matti Airas <matti.p.airas@nokia.com> | 2010-09-10 23:01:39 +0300 |
commit | 29b8ce00c0729106ac99c67004d3f2966560e398 (patch) | |
tree | 87aa54afc2892ad1b752933e63f8267aca712352 /examples/network | |
parent | 8047c886010e770b17cc2d25546c9a9eb0b4499a (diff) |
merged fortuneserver.py from PyQt
Diffstat (limited to 'examples/network')
-rwxr-xr-x | examples/network/fortuneserver.py | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/examples/network/fortuneserver.py b/examples/network/fortuneserver.py index 1725715..1782b3e 100755 --- a/examples/network/fortuneserver.py +++ b/examples/network/fortuneserver.py @@ -2,73 +2,82 @@ """PySide port of the network/fortuneserver example from Qt v4.x""" -import sys import random + from PySide import QtCore, QtGui, QtNetwork class Server(QtGui.QDialog): def __init__(self, parent=None): - QtGui.QDialog.__init__(self, parent) + super(Server, self).__init__(parent) - self.statusLabel = QtGui.QLabel() - self.quitButton = QtGui.QPushButton(self.tr("Quit")) - self.quitButton.setAutoDefault(False) + statusLabel = QtGui.QLabel() + quitButton = QtGui.QPushButton("Quit") + quitButton.setAutoDefault(False) self.tcpServer = QtNetwork.QTcpServer(self) if not self.tcpServer.listen(): - QtGui.QMessageBox.critical(self, self.tr("Fortune Server"), - self.tr("Unable to start the server: %(error)s.") - % {'error': self.tcpServer.errorString()}) + QtGui.QMessageBox.critical(self, "Fortune Server", + "Unable to start the server: %s." % self.tcpServer.errorString()) self.close() return - self.statusLabel.setText(self.tr("The server is running on port %(port)d.\n" - "Run the Fortune Client example now.") - % {'port': self.tcpServer.serverPort()}) + statusLabel.setText("The server is running on port %d.\nRun the " + "Fortune Client example now." % self.tcpServer.serverPort()) - self.fortunes = [ - self.tr("You've been leading a dog's life. Stay off the furniture."), - self.tr("You've got to think about tomorrow."), - self.tr("You will be surprised by a loud noise."), - self.tr("You will feel hungry again in another hour."), - self.tr("You might have mail."), - self.tr("You cannot kill time without injuring eternity."), - self.tr("Computers are not intelligent. They only think they are."), - ] + self.fortunes = ( + "You've been leading a dog's life. Stay off the furniture.", + "You've got to think about tomorrow.", + "You will be surprised by a loud noise.", + "You will feel hungry again in another hour.", + "You might have mail.", + "You cannot kill time without injuring eternity.", + "Computers are not intelligent. They only think they are.") - self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("close()")) - self.connect(self.tcpServer, QtCore.SIGNAL("newConnection()"), self.sendFortune) + quitButton.clicked.connect(self.close) + self.tcpServer.newConnection.connect(self.sendFortune) buttonLayout = QtGui.QHBoxLayout() buttonLayout.addStretch(1) - buttonLayout.addWidget(self.quitButton) + buttonLayout.addWidget(quitButton) + buttonLayout.addStretch(1) mainLayout = QtGui.QVBoxLayout() - mainLayout.addWidget(self.statusLabel) + mainLayout.addWidget(statusLabel) mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) - self.setWindowTitle(self.tr("Fortune Server")) + self.setWindowTitle("Fortune Server") def sendFortune(self): block = QtCore.QByteArray() out = QtCore.QDataStream(block, QtCore.QIODevice.WriteOnly) out.setVersion(QtCore.QDataStream.Qt_4_0) out.writeUInt16(0) - out.writeString(self.fortunes[random.randint(0, len(self.fortunes) - 1)]) + fortune = self.fortunes[random.randint(0, len(self.fortunes) - 1)] + + try: + # Python v3. + fortune = bytes(fortune, encoding='ascii') + except: + # Python v2. + pass + + out.writeString(fortune) out.device().seek(0) out.writeUInt16(block.size() - 2) clientConnection = self.tcpServer.nextPendingConnection() - self.connect(clientConnection, QtCore.SIGNAL("disconnected()"), - clientConnection, QtCore.SLOT("deleteLater()")) + clientConnection.disconnected.connect(clientConnection.deleteLater) clientConnection.write(block) clientConnection.disconnectFromHost() if __name__ == '__main__': + + import sys + app = QtGui.QApplication(sys.argv) server = Server() random.seed(None) |