def __init__(self, parent: QWidget = None): super().__init__(parent) self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint) self.server = QLocalServer(self) if not self.server.listen("fortune"): QMessageBox.critical( self, self.tr("Local Fortune Server"), self.tr("Unable to start the server: %s." % (self.server.errorString())), ) QTimer.singleShot(0, self.close) return statusLabel = QLabel() statusLabel.setWordWrap(True) statusLabel.setText( self. tr("The server is running.\nRun the Local Fortune Client example now." )) 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."), ) quitButton = QPushButton(self.tr("Quit")) quitButton.setAutoDefault(False) quitButton.clicked.connect(self.close) self.server.newConnection.connect(self.sendFortune) buttonLayout = QHBoxLayout() buttonLayout.addStretch(1) buttonLayout.addWidget(quitButton) buttonLayout.addStretch(1) mainLayout = QVBoxLayout(self) mainLayout.addWidget(statusLabel) mainLayout.addLayout(buttonLayout) self.setWindowTitle(QGuiApplication.applicationDisplayName())
def __init__(self, parent: QWidget = None): super().__init__(parent) self._in = QDataStream() self.blockSize = 0 self.currentFortune = "" self.hostLineEdit = QLineEdit("fortune") self.getFortuneButton = QPushButton(self.tr("Get Fortune")) self.statusLabel = QLabel( self.tr( "This examples requires that you run the Local Fortune Server example as well." ) ) self.socket = QLocalSocket() self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint) hostLabel = QLabel(self.tr("&Server name:")) hostLabel.setBuddy(self.hostLineEdit) self.statusLabel.setWordWrap(True) self.getFortuneButton.setDefault(True) quitButton = QPushButton(self.tr("Quit")) buttonBox = QDialogButtonBox() buttonBox.addButton(self.getFortuneButton, QDialogButtonBox.ActionRole) buttonBox.addButton(quitButton, QDialogButtonBox.RejectRole) self._in.setDevice(self.socket) self._in.setVersion(QDataStream.Qt_5_10) self.hostLineEdit.textChanged.connect(self.enableGetFortuneButton) self.getFortuneButton.clicked.connect(self.requestNewFortune) quitButton.clicked.connect(self.close) self.socket.readyRead.connect(self.readFortune) self.socket.errorOccurred.connect(self.displayError) mainLayout = QGridLayout(self) mainLayout.addWidget(hostLabel, 0, 0) mainLayout.addWidget(self.hostLineEdit, 0, 1) mainLayout.addWidget(self.statusLabel, 2, 0, 1, 2) mainLayout.addWidget(buttonBox, 3, 0, 1, 2) self.setWindowTitle(QGuiApplication.applicationDisplayName()) self.hostLineEdit.setFocus()