示例#1
0
class QSingleApplication(QApplication):
    def singleStart(self, mainWindow):
        self.mainWindow = mainWindow
        # Socket
        self.m_socket = QLocalSocket()
        self.m_socket.connected.connect(self.connectToExistingApp)
        self.m_socket.error.connect(self.startApplication)
        self.m_socket.connectToServer(self.applicationName(), QIODevice.WriteOnly)
    def connectToExistingApp(self):
        if len(sys.argv)>1 and sys.argv[1] is not None:
            self.m_socket.write(sys.argv[1])
            self.m_socket.bytesWritten.connect(self.quit)
        else:
            QMessageBox.warning(None, self.tr("Already running"), self.tr("The program is already running."))
            # Quit application in 250 ms
            QTimer.singleShot(250, self.quit)
    def startApplication(self):
        self.m_server = QLocalServer()
        if self.m_server.listen(self.applicationName()):
            self.m_server.newConnection.connect(self.getNewConnection)
            self.mainWindow.show()
        else:
            QMessageBox.critical(None, self.tr("Error"), self.tr("Error listening the socket."))
    def getNewConnection(self):
        self.new_socket = self.m_server.nextPendingConnection()
        self.new_socket.readyRead.connect(self.readSocket)
    def readSocket(self):
        f = self.new_socket.readLine()
        self.mainWindow.getArgsFromOtherInstance(str(f))
        self.mainWindow.activateWindow()
        self.mainWindow.show()
class QSingleApplication(QApplication):
    # Signal sent when another instance is launched with arguments
    argsReceived = QtCore.Signal((str, ))

    # Start the application,
    # either as a server (first instance) or as a client
    # others clients only send the argv they have been given and exit
    def singleStart(self, mainWindow):
        print "singleStart() function from QSingleApp got called."
        self.mainWindow = mainWindow
        # Socket
        self.m_socket = QLocalSocket()
        # Connected, error are signals that are being emitted
        self.m_socket.connected.connect(self.connectToExistingApp)
        self.m_socket.error.connect(self.startApplication)
        self.m_socket.connectToServer(self.applicationName(),
                                      QIODevice.WriteOnly)

    # used for the very first instance to create the main window and start server
    def startApplication(self):
        self.m_server = QLocalServer()
        print "startApplication() function from QSingleApplication got called"
        # Returns bool (True on success). Tells server to listen for incoming connections on 'name'
        if self.m_server.listen(self.applicationName()):
            print "Server is listening .... startApplication() function from QSingleApp"
            self.m_server.newConnection.connect(self.getNewConnection)
            # After emitting a signal, connecting it to the UI class function
            self.argsReceived.connect(self.mainWindow.receive_args)
            self.mainWindow.show()
        else:
            QMessageBox.critical(None, self.tr("Error"),
                                 self.tr("Error listening the socket."))

    # used by later instances to send argv to the main instance
    def connectToExistingApp(self):
        if len(sys.argv) > 1 and sys.argv[1] is not None:
            self.m_socket.write(sys.argv[1])
            print "exiting new app A"
            self.m_socket.bytesWritten.connect(self.quit)
        else:
            print "exiting new app B"
            self.m_socket.write("--show")
            self.m_socket.bytesWritten.connect(self.quit)

    def getNewConnection(self):
        print "getNewConnection() was called"
        self.mainWindow.show()
        self.new_socket = self.m_server.nextPendingConnection()
        self.new_socket.readyRead.connect(self.readSocket)

    def readSocket(self):
        print "readSocket() function in QSingleApplication.py"
        f = self.new_socket.readLine()
        self.argsReceived.emit(str(f))
示例#3
0
class QSingleApplication(QApplication):
    # Signal sent when another instance is launched with arguments
    argsReceived = QtCore.Signal((str,))

    # Start the application,
    # either as a server (first instance) or as a client
    # others clients only send the argv they have been given and exit
    def singleStart(self, mainWindow):
        self.mainWindow = mainWindow
        # Socket
        self.m_socket = QLocalSocket()
        self.m_socket.connected.connect(self.connectToExistingApp)
        self.m_socket.error.connect(self.startApplication)
        self.m_socket.connectToServer(self.applicationName(), QIODevice.WriteOnly)

    # used for the very first instance to create the main window and start server
    def startApplication(self):
        self.m_server = QLocalServer()
        if self.m_server.listen(self.applicationName()):
            self.m_server.newConnection.connect(self.getNewConnection)
            self.argsReceived.connect(self.mainWindow.receive_args)
            self.mainWindow.show()
        else:
            QMessageBox.critical(None, self.tr("Error"), self.tr("Error listening the socket."))

    # used by later instances to send argv to the main instance
    def connectToExistingApp(self):
        if len(sys.argv)>1 and sys.argv[1] is not None:
            self.m_socket.write(sys.argv[1])
            self.m_socket.bytesWritten.connect(self.quit)
        else:
            self.m_socket.write("--show")
            self.m_socket.bytesWritten.connect(self.quit)

    def getNewConnection(self):
        self.mainWindow.show()
        self.new_socket = self.m_server.nextPendingConnection()
        self.new_socket.readyRead.connect(self.readSocket)

    def readSocket(self):
        f = self.new_socket.readLine()
        self.argsReceived.emit(str(f))