Run only one instance of a Qt application
Article Metadata
Tested with
Compatibility
Article
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;
socket.connectToServer(serverName);
if (socket.waitForConnected(500))
return; // Exit already a process running
m_localServer = new QLocalServer(this);
connect(m_localServer, SIGNAL(newConnection()), this, SLOT(newLocalSocketConnection()));
m_localServer->listen(serverName);
- 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
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
sharedMemory.setKey(UniqueID);
if(sharedMemory.attach()) {
m_isRunning = true;
return;
}
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 http://doc.qt.nokia.com/solutions/4/qtsingleapplication/qtsingleapplication.html#details .
int main(int argc, char **argv)
{
QtSingleApplication app(argc, argv);
if (app.isRunning())
return 0;
MyMainWidget mmw;
app.setActivationWindow(&mmw);
mmw.show();
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)


Better presentation! Keep up the good work.
croozeus 11:02, 25 November 2010 (UTC)
Dooorgham - QLocalSocket issue
in the solution of local socket, I had some issue when the qlocalserver crashes for any reson it does not release the socket file and any further trying to connect fail cause he sees that some app is already listening on that socket, solving this issue by manualy deleting the socket temp file solve the issue of crash case but kill the idea of single application cause you can start two servers concurrently this way ... any suggestions ??dooorgham 20:52, 8 April 2013 (EEST)
Hamishwillee - Might be best to post on discussion board or private message the users
This article is 3 years old and the author may no longer be watching it, or even involved in the community. I suggest perhaps you try private messaging them (hover over their name in the Article Metadata). Even better, post a request in the Qt discussion boards, linking to this article.
Regards
Hamishhamishwillee 08:20, 15 April 2013 (EEST)