本文整理汇总了Python中PyQt5.QtNetwork.QLocalSocket.abort方法的典型用法代码示例。如果您正苦于以下问题:Python QLocalSocket.abort方法的具体用法?Python QLocalSocket.abort怎么用?Python QLocalSocket.abort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtNetwork.QLocalSocket
的用法示例。
在下文中一共展示了QLocalSocket.abort方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import abort [as 别名]
class Client:
def __init__(self):
self.socket = QLocalSocket()
self.socket.setServerName(piony.G_SOCKET_NAME)
self.socket.error.connect(self.displayError)
self.socket.disconnected.connect(self.socket.deleteLater)
def _log_(self, text, *args):
logger.info(self.__class__.__qualname__ + ': ' + text, *args)
def connect(self):
self.socket.abort()
self._log_("connection attempt")
self.socket.connectToServer()
def send(self, argv):
data = QByteArray()
out = QDataStream(data, QIODevice.WriteOnly)
out.setVersion(QDataStream.Qt_5_0)
out.writeQVariant(argv)
self._log_("writes: %s", str(data))
self.socket.write(data)
self.socket.flush()
self.socket.disconnectFromServer()
def displayError(self, err):
msg = {
QLocalSocket.ServerNotFoundError:
"The host was not found. Check the host name and port.",
QLocalSocket.ConnectionRefusedError:
"The connection was refused by the peer. "
"Check server is running, it's host and port.",
QLocalSocket.PeerClosedError:
"Peer was closed", # None,
}.get(err, "Client error: {}.".format(self.socket.errorString()))
self._log_(msg)
示例2: Client
# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import abort [as 别名]
class Client(QDialog):
def __init__(self, parent=None):
super(Client, self).__init__(parent)
self.blockSize = 0
self.currentFortune = None
hostLabel = QLabel("&Server name:")
self.hostLineEdit = QLineEdit("fortune")
hostLabel.setBuddy(self.hostLineEdit)
self.statusLabel = QLabel("This examples requires that you run the Fortune Server " "example as well.")
self.statusLabel.setWordWrap(True)
self.getFortuneButton = QPushButton("Get Fortune")
self.getFortuneButton.setDefault(True)
quitButton = QPushButton("Quit")
buttonBox = QDialogButtonBox()
buttonBox.addButton(self.getFortuneButton, QDialogButtonBox.ActionRole)
buttonBox.addButton(quitButton, QDialogButtonBox.RejectRole)
self.socket = QLocalSocket()
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.error.connect(self.displayError)
mainLayout = QGridLayout()
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.setLayout(mainLayout)
self.setWindowTitle("Fortune Client")
self.hostLineEdit.setFocus()
def requestNewFortune(self):
self.getFortuneButton.setEnabled(False)
self.blockSize = 0
self.socket.abort()
self.socket.connectToServer(self.hostLineEdit.text())
def readFortune(self):
ins = QDataStream(self.socket)
ins.setVersion(QDataStream.Qt_4_0)
if self.blockSize == 0:
if self.socket.bytesAvailable() < 2:
return
self.blockSize = ins.readUInt16()
if ins.atEnd():
return
nextFortune = ins.readQString()
if nextFortune == self.currentFortune:
QTimer.singleShot(0, self.requestNewFortune)
return
self.currentFortune = nextFortune
self.statusLabel.setText(self.currentFortune)
self.getFortuneButton.setEnabled(True)
def displayError(self, socketError):
errors = {
QLocalSocket.ServerNotFoundError: "The host was not found. Please check the host name and port "
"settings.",
QLocalSocket.ConnectionRefusedError: "The connection was refused by the peer. Make sure the "
"fortune server is running, and check that the host name and "
"port settings are correct.",
QLocalSocket.PeerClosedError: None,
}
msg = errors.get(socketError, "The following error occurred: %s." % self.socket.errorString())
if msg is not None:
QMessageBox.information(self, "Fortune Client", msg)
self.getFortuneButton.setEnabled(True)
def enableGetFortuneButton(self):
self.getFortuneButton.setEnabled(self.hostLineEdit.text() != "")