How to make the QT application have only one instance

Source: Internet
Author: User

In practical applications, we often need to make the application have only one instance. When opening a new document or page, we only need to replace the current window or open a new tag, instead of restarting the application once. In QT, can we do this? The answer is yes, because QT itself can directly call the system API and certainly can, but we hope to find a universal cross-platform solution.

This requires qt's qlocalsocket and qlocalserver classes. These two classes are no different from the network communication socket in terms of interfaces, but they are not really network APIs, just imitating them. These two classes are implemented using Unix domain socket on UNIX/Linux systems, while named pipe is used in windows.

Since it is a network API, the idea is simple. When an application starts, it will first connect to a server (which is identified by the Application name, just like a network port ), if the connection fails, the server is the first instance. Otherwise, the startup parameter is sent to the server and then automatically exits. The server will process the connection after receiving the notification.

These actions should be put after the application instance is created, because QT itself has many operations that cannot be operated without the application instance (at least the event loop can be started after the application is created ), therefore, the best position is to inherit from qapplicaiton or qcoreapplication to customize a yourownapplication and then proceed in the constructor. The following is a diagram.

The first is the yourownapplication constructor:

 

Qstring servername = qcoreapplication: applicationname ();

Qlocalsocket socket;

Socket. connecttoserver (servername );

If (socket. waitforconnected (500) {// if it can be connected, send the parameter to the server and then exit

Qtextstream stream (& socket );

Qstringlist ARGs = qcoreapplication: arguments ();

If (ARGs. Count ()> 1)

Stream <args. Last ();

Else

Stream <qstring ();

Stream. Flush ();

Socket. waitforbyteswritten ();

Qapp-> quit ();

Return;

}

// Run here, indicating that no instance is running, so create a server.

M_localserver = new qlocalserver (this );

Connect (m_localserver, signal (newconnection ()),

This, slot (newlocalsocketconnection (); // listen for new connections

If (! M_localserver-> listen (servername )){

If (m_localserver-> servererror () = q1_actsocket: addressinuseerror

& Qfile: exists (m_localserver-> servername () {// ensure that the listener is successful

Qfile: Remove (m_localserver-> servername ());

M_localserver-> listen (servername );

}

}

 

This ensures that the newly started program automatically exits when detecting other instances, but the requests sent by the new program are not processed. Let's take a look at the processing function, that is, newlocalsocketconnection () in the previous code ().

Qlocalsocket * socket = m_localserver->Nextpendingconnection();

If(! Socket)

Return;

Socket->Waitforreadyread(1000 );

Qtextstream stream (socket );

... ... // Other processing

DeleteSocket;

Mainwindow ()->Raise();

Mainwindow ()->Activatewindow(); // Remember to activate the window.

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.