Qt Single Instance solution for pyside QT 4.7

Source: Internet
Author: User

It is said that QT originally had such a thing as qtsolution, and later updates were not timely.

No corresponding QT 4.7 version.

Google found that there was an antidote on the pyside official website.

Http://developer.qt.nokia.com/forums/viewthread/5733

The following is a general solution description.

// Try to connect to your server
M_socket = qlocalsocket ()
M_socket.connecttoserver (qapp. applicationname (), qiodevice. readonly)

// If this fail is because the app is not running then create the server, then in the next time the connect will work
M_server = qlocalserver ()
M_server = qlocalserver. Listen (qapp. applicationname ())

Try a full version later.

##########################

Let's take a closer look at the last line of the page. someone already has a solution.

Http://www.dallagnese.fr/en/computers-it/recette-python-qt4-qsingleapplication-pyside/

##########################

 """ 
The MIT license

Copyright 2011 Thomas Dall 'agnese <thomas.dallagnese@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
Of this software and associated documentation files (the "software"), to deal
In the software without restriction, including without limitation the rights
To use, copy, modify, merge, publish, distribute, sublicense, and/or decrypt
Copies of the software, and to permit persons to whom the software is
Furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be supported in
All copies or substantial portions of the software.

The software is provided "as is", without warranty of any kind, express or
Implied, including but not limited to the warranties of merchantability,
Fitness for a particle purpose and noninfringement. In no event shall
Authors or copyright holders be liable for any claim, damages or other
Liability, whether in an action of contract, tort or otherwise, arising from,
Out of or in connection with the software or the use or other dealings in
The software.

"""
_ Author __ = " Thomas Dall 'agnese "
_ Email __ = " Thomas.dallagnese@gmail.com "
_ Version __ = " 1.0 "
_ URL __ = " Http://www.dallagnese.fr "

From Pyside. qtgui Import Qmessagebox, qapplication
From Pyside. qtcore Import Qiodevice, qtimer, qcoreapplication
From Pyside. qtnetwork Import Qlocalserver, qlocalsocket
Import Sys

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 ()

If _ Name __ = ' _ Main __ ' :
From Pyside. qtgui Import Qmainwindow, qlabel
Class Dallagnesewindow (qmainwindow ):
Def _ Init __ (Self ):
Qmainwindow. _ Init __ (Self)
Self. setwindowtitle ( " Qsingleapplication demo " )
Labeltext = " <B> dallagnese. fr recipe </B> <br/> \
Allows you to start your program only once. <br/> \
Parameters of later cballs can be handled by this application. "
Self. setcentralwidget (qlabel (labeltext ))
Def Getargsfromotherinstance (self, argS ):
Qmessagebox. Information (self, self. tr ( " Received ARGs from another instance " ), ArgS)
App = Qsingleapplication (SYS. argv)
App. setapplicationname ( " Dallagnese. fr recipe 01 " )
Mywindow = Dallagnesewindow ()
App. singlestart (mywindow)
Sys.exit(app.exe C _())

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.