Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT single instance application
Forum Updated to NodeBB v4.3 + New Features

QT single instance application

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 19.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Priya
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        0
        • SGaistS SGaist

          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

          P Offline
          P Offline
          Priya
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • TheBadgerT Offline
            TheBadgerT Offline
            TheBadger
            wrote on last edited by TheBadger
            #5

            @Priya Not sure where you are at but from my understanding to use it (not tested):

            The qt-solutions repo contains the QtSingleApplication code

            1. Get the sources from the code.qt.io repo:
              • git clone git://code.qt.io/qt-solutions/qt-solutions.git
            2. Open the qt-solutions/qtsingleapplication folder
              • Should be part of the code you just checked out.
            3. 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.
            4. In the pro file of your application add the Pri file of the QSingleApplication code that you need
              • src/qtsingleapplication.pri for widget application
              • src/qtsinglecoreapplication.pri for command line application
            5. 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


            Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

            1 Reply Last reply
            2
            • FranckynosF Offline
              FranckynosF Offline
              Franckynos
              wrote on last edited by
              #6

              @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 !!!

              P 1 Reply Last reply
              4
              • FranckynosF Franckynos

                @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 !!!

                P Offline
                P Offline
                Priya
                wrote on last edited by
                #7

                @Franckynos

                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

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  onek24
                  wrote on last edited by onek24
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved