Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8624967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:39:50+00:00 2026-06-12T07:39:50+00:00

Is there a Python version of the C++ class QtSingleApplication from Qt Solutions ?

  • 0

Is there a Python version of the C++ class QtSingleApplication from Qt Solutions?

QtSingleApplication is used to make sure that there can never be more than one instance of an application running at the same time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:39:52+00:00Added an answer on June 12, 2026 at 7:39 am

    Here is my own implementation.
    It has been tested with Python 2.7 and PySide 1.1.

    It has essentially the same interface as the C++ version of QtSingleApplication. The main difference is that you must supply an application unique id to the constructor. (The C++ version by default uses the path to the executable as a unique id; that would not work here because the executable will most likely be 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)
    

    Here is a simple test program:

    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_())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to upgrade the version of Python used in a virtual
Is there a Python library that can parse an CSS selector and emit an
Is there a way I can make class decorators work on Google App Engine
In Python is there any way to make a class, then make a second
Are there Python 3 bindings for Clutter? If so, how can I get them
Are there any Python JSON parsers that will cope with trailing commas? (I'm consuming
Is there a python module that will do a waterfall plot like MATLAB does?
Is there a version of the PHP array class where all the elements must
I am updating an old project that used an old version of cjson to
Python version 2.7. Scenario class A(object): x = 0 class B(A): pass ai =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.