I am very new to the pyqt. I am in the process of developing the app. I did the basic app I am using following code to prevent app opens multiple times
''' Class to check weather app is already open or not '''
class SingleApplication(QtWidgets.QApplication):
messageAvailable = QtCore.pyqtSignal(object)
def __init__(self, argv, key):
super().__init__(argv)
# cleanup (only needed for unix)
QtCore.QSharedMemory(key).attach()
self._memory = QtCore.QSharedMemory(self)
self._memory.setKey(key)
if self._memory.attach():
self._running = True
else:
self._running = False
if not self._memory.create(1):
raise RuntimeError(self._memory.errorString())
def isRunning(self):
return self._running
if __name__ == '__main__':
key = common.appTitle
app = SingleApplication(sys.argv,key)
print(app.isRunning())
if app.isRunning():
print("App is already running")
sys.exit(1)
else:
appctxt = ApplicationContext() # 1. Instantiate ApplicationContext
window = Mainwindow()
QApplication.setQuitOnLastWindowClosed(False) ## prevent to close while close message box at the time of background running
mainwindow = window.runwindow()
mainwindow.show()
exit_code = appctxt.app.exec_() # 2. Invoke appctxt.app.exec_()
sys.exit(exit_code)
The above code prevents multiple opening of the App. If we open app multiple it prevent with log message of "App is already running". I need to focus or activatedwindow already opened app window while clicking the app icon if an app is already in an open state. Please guide me. Thank you