gpt4 book ai didi

python - PySide 或 PyQt 的 QtSingleApplication

转载 作者:太空狗 更新时间:2023-10-30 00:25:23 27 4
gpt4 key购买 nike

是否有C++类的Python版本QtSingleApplication来自 Qt Solutions

QtSingleApplication用于确保永远不会有多个应用程序实例同时运行。

最佳答案

这是我自己的实现。它已通过 Python 2.7 和 PySide 1.1 测试。

它的界面与C++ version of QtSingleApplication 基本相同。 .主要区别在于您必须向构造函数提供一个应用程序唯一 ID。 (默认情况下,C++ 版本使用可执行文件的路径作为唯一 ID;这在这里不起作用,因为可执行文件很可能是 python.exe。)

from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtNetwork import *

class QtSingleApplication(QApplication):

messageReceived = Signal(unicode)

def __init__(self, id, *argv):

super(QtSingleApplication, 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)
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)

这是一个简单的测试程序:

import sys
from PySide.QtGui import *
from QtSingleApplication import QtSingleApplication

appGuid = 'F3FF80BA-BA05-4277-8063-82A6DB9245A2'
app = QtSingleApplication(appGuid, sys.argv)
if app.isRunning(): sys.exit(0)

w = QWidget()
w.show()
app.setActivationWindow(w)
sys.exit(app.exec_())

关于python - PySide 或 PyQt 的 QtSingleApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12712360/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com