def __init__(self, id, *argv): super(QSingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection)
def _onNewConnection(self): if self._inSocket: self._inSocket.readyRead.disconnect(self._onReadyRead) self._inSocket = self._server.nextPendingConnection() if not self._inSocket: return self._inStream = QTextStream(self._inSocket) self._inStream.setCodec('UTF-8') self._inSocket.readyRead.connect(self._onReadyRead) if self._activateOnMessage: self.activateWindow()
def main(): import sys app = QCoreApplication(sys.argv) # noqa: F841 f = QFile() f.open(sys.stdout.fileno(), QIODevice.WriteOnly) out = QTextStream(f) serialPortInfos = QSerialPortInfo.availablePorts() out << "Total number of ports available: " << len(serialPortInfos) << "\n" blankString = "N/A" description = "" manufacturer = "" serialNumber = "" for serialPortInfo in serialPortInfos: description = serialPortInfo.description() manufacturer = serialPortInfo.manufacturer() serialNumber = serialPortInfo.serialNumber() out << "\nPort: " << serialPortInfo.portName( ) << "\nLocation: " << serialPortInfo.systemLocation( ) << "\nDescription: " << ( # noqa: E501 description if description else blankString ) << "\nManufacturer: " << ( manufacturer if manufacturer else blankString) << "\nSerial number: " << ( serialNumber if serialNumber else blankString ) << "\nVendor Identifier: " << ( QByteArray.number(serialPortInfo.vendorIdentifier(), 16) if serialPortInfo.hasVendorIdentifier() else blankString ) << "\nProduct Identifier: " << ( QByteArray.number(serialPortInfo.productIdentifier(), 16) if serialPortInfo.hasProductIdentifier() else blankString) << "\n"
def onFileSave(self): if not self.m_filePath: self.onFileSaveAs() return f = QFile(self.m_filePath) if not f.open(QIODevice.WriteOnly | QIODevice.Text): QMessageBox.warning( self, self.windowTitle(), self.tr("Could not write to file %s: %s" % (QDir.toNativeSeparators( self.m_filePath), f.errorString())), ) return text = QTextStream(f) text << self.editor.toPlainText() self.editor.document().setModified(False)
class QSingleApplication(QApplication): messageReceived = Signal() def __init__(self, id, *argv): super(QSingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection) def isRunning(self): return self._isRunning def id(self): return self._id def activationWindow(self): return self._activationWindow def setActivationWindow(self, activationWindow, activateOnMessage=True): self._activationWindow = activationWindow self._activateOnMessage = activateOnMessage def activateWindow(self): if not self._activationWindow: return self._activationWindow.setWindowState(( self._activationWindow.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive) SetWindowPos( self._activationWindow.winId(), win32con. HWND_TOPMOST, # = always on top. only reliable way to bring it to the front on windows 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) SetWindowPos( self._activationWindow.winId(), win32con. HWND_NOTOPMOST, # disable the always on top, but leave window at its top position 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # self._activationWindow.raise_() self._activationWindow.activateWindow() def sendMessage(self, msg): if not self._outStream: return False self._outStream << msg << '\n' self._outStream.flush() return self._outSocket.waitForBytesWritten() def _onNewConnection(self): if self._inSocket: self._inSocket.readyRead.disconnect(self._onReadyRead) self._inSocket = self._server.nextPendingConnection() if not self._inSocket: return self._inStream = QTextStream(self._inSocket) self._inStream.setCodec('UTF-8') self._inSocket.readyRead.connect(self._onReadyRead) if self._activateOnMessage: self.activateWindow() def _onReadyRead(self): while True: msg = self._inStream.readLine() if not msg: break self.messageReceived.emit(msg)