QT single instance application
-
Hi,
I am using QT 5.5 version. I have created a QT application. I want this application to work as single instance application. In some forum mentioned that QtSingleApplication will not work for QT5 and above. Is there any dll which i need to download form QT to make the application to run as single instance application. Please let me know the steps to achieve single instance application in QT 5.5.
Please reply me as soon as possible.Thanks & Regards,
Priya -
The idea of QtSingleApplication is straight forward I believe. Each instance of a program on startup tries connect to a named server. If the connection cannot be made then you are the first running instance of the program and you create the named server and startup the program normally. If you can connect the program is already running and you gracefully exit (and preferably set focus to the active program). QLocalServer and QLocalSocket are probably what you need to look at in order to make this work.
-
Hi and welcome to devnet,
Always check the source ;)
Looking at the official repository, there was a fix to build QtSingleApplication with Qt 5.5 so you can use it for your application with Qt 5.
It builds fine on OS X
-
@SGaist
Can i Use #include <QTSingleApplicatino>?
Actually am not finding this Class. I am trying this single instance in Linux OS.
Please tell me What is the fix in QT 5.5
How can i use single instance application.Thanks & Regards,
Priya -
@Priya Not sure where you are at but from my understanding to use it (not tested):
The qt-solutions repo contains the QtSingleApplication code
- Get the sources from the code.qt.io repo:
git clone git://code.qt.io/qt-solutions/qt-solutions.git
- Open the
qt-solutions/qtsingleapplication
folder- Should be part of the code you just checked out.
- Build the library.
- Read the README.TXT or INSTALL.txt to build the files in the 'buildlib' folder
- Something like open the
qt-solutions/qtsingleapplication/buildlib/buildlib.pro
file with Qt Creator and build.
- In the pro file of your application add the Pri file of the QSingleApplication code that you need
src/qtsingleapplication.pri
for widget applicationsrc/qtsinglecoreapplication.pri
for command line application
- include the headers and use
- Check the examples and other documentation.
Edit: For the change look at the last commit on the repo website: http://code.qt.io/cgit/qt-solutions/qt-solutions.git/
Hope that helps
- Get the sources from the code.qt.io repo:
-
@SGaist said:
ng at the official repository
Or you can do this : =>
in main !!!
Application a(argc, argv); // !!! et pas QApp ... // Only ONE instance if(!a.lock()) return -42; MainWidget u(argc, argv, splash); u.show(); return a.exec();
application.h
class Application : public QApplication { Q_OBJECT public: Application(int &argc, char **argv); ~Application(); bool lock(); private: QSharedMemory *_singular; // shared memory !! SINGLE ACCESS };
application.cpp
Application::Application(int &argc, char **argv):QApplication(argc, argv, true) { _singular = new QSharedMemory("SharedMemoryForOnlyOneInstanceOfYourBeautifulApplication", this); } Application::~Application() { if(_singular->isAttached()) _singular->detach(); } bool Application::lock() { if(_singular->attach(QSharedMemory::ReadOnly)){ _singular->detach(); return false; } if(_singular->create(1)) return true; return false; }
AND IT WORKS PERFECTLY !!!
-
Yes it is working. Thank you.
But i want some action to be taken in First application when second tries to launch.
Means i want the communication between the two instance.
Is that possible.
Please let me know how we can achieve this.Thanks & Regards,
Priya -
Check this out (Github: SingleApplication)
The first instance of the application starts a local server which recieves a connection-request if another instance of the application is launched because the instance is trying to connect to the local server if there is already one running.