Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 01:18, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Run only one instance of a Qt application

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Emulator / desktop / device
Platform(s): All Qt Supported
Keywords: Qt Application
Created: skumar_rao (24 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

when we try running a Qt console application there is always a issue to take care is that out console application is not running multiple instances which might not be desirable in common application requirement conditions. In this article we will try more then one way of avoiding this problem, each solution is suitable for a particular situation so we have to try and use the suitable solution when required.

  • Using QLocalSocket
  • Using QSharedMemory
  • Using QtSingleApplication

Using QLocalSocket

This approach creates a QLocalSocket on 1st instance then when ever a new instance starts we will try connecting to the same LocalSocket created if we are successful in connecting to that socket then there is a instance of the same process running.

QLocalSocket socket;
if (socket.waitForConnected(500))
return; // Exit already a process running
m_localServer = new QLocalServer(this);
connect(m_localServer, SIGNAL(newConnection()), this, SLOT(newLocalSocketConnection()));
  • You create a QLocalSocket and connect to the server specified by the serverName;
  • Create a QLocalServer and listen for the connections;
  • Before starting the application check how much connections you are listening already, if there is at least one that means you have opened an application and you shouldn't let another one to open

Using QSharedMemory

Same as previous in this approach we will create a shared memory then on each instance starting we will try checking if we can create a shared memory with same UniqueID, if we can't create a shared memory then mostly a instance of the same process running which already created the shared memory.

QSharedMemory sharedMemory
if(sharedMemory.attach()) {
m_isRunning = true;
if (!sharedMemory.create(1)) {
return; // Exit already a process running

Using QtSingleApplication

It was introduced as a Utility solution from Qt but has been removed for unknown reasons. still you can find API reference in .

 int main(int argc, char **argv)
QtSingleApplication app(argc, argv);
if (app.isRunning())
return 0;
MyMainWidget mmw;
return app.exec();

In-spite of the fact that above QtSingleApplication based code looks very clean approach then others, I must confess that I was not able to compile using 4.6.3 or 4.7

--skumar_rao 16:54, 24 November 2010 (UTC)

This page was last modified on 11 October 2012, at 01:18.
388 page views in the last 30 days.