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. Qt5 prevent multiple instances of application
Forum Updated to NodeBB v4.3 + New Features

Qt5 prevent multiple instances of application

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 7 Posters 8.6k Views 3 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.
  • A Offline
    A Offline
    alecs26
    wrote on last edited by
    #3

    @Vinod-Kuntoji Wow, that's nice, thank you so much for you fast answer !
    Alex

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

      Hi,

      Don't get me wrong but you are basing your assumption on a 5 years old answer where it's nowhere stated that it's not working with Qt 5. In fact, Qt 5 is not discussed at all on that thread and the original poster found out a bug in its own application which was not related to that module.

      Now back to your problem: QtSingleApplication works with Qt 5.

      One of the advantages of QtSingleApplication is that you can more easily communicate with the original instance of the application.

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

      1 Reply Last reply
      0
      • Vinod KuntojiV Vinod Kuntoji

        @alecs26 ,

        Inside the main(), put this
        QApplication a(argc, argv);

        QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");
        if( !shared.create( 512, QSharedMemory::ReadWrite) )
        {
        qWarning() << "Can't start more than one instance of the application.";
        exit(0);
        }
        else {
        qDebug() << "Application started successfully.";
        }

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #5

        @Vinod-Kuntoji said in Qt5 prevent multiple instances of application:

        @alecs26 ,

        Inside the main(), put this
        QApplication a(argc, argv);

        QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");

        I assume that is supposed to be a unique GUID/identifier per application? The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another.... Assuming that is correct, I think you ought tell the OP/others that a distinct string must be generated per each application, don't you think?

        See instead https://stackoverflow.com/a/33526851/489865, for an implementation of this principle which does not "clash" with others.

        Otherwise @SGaist's QtSingleApplication may be the way to go.

        raven-worxR 1 Reply Last reply
        2
        • JonBJ JonB

          @Vinod-Kuntoji said in Qt5 prevent multiple instances of application:

          @alecs26 ,

          Inside the main(), put this
          QApplication a(argc, argv);

          QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");

          I assume that is supposed to be a unique GUID/identifier per application? The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another.... Assuming that is correct, I think you ought tell the OP/others that a distinct string must be generated per each application, don't you think?

          See instead https://stackoverflow.com/a/33526851/489865, for an implementation of this principle which does not "clash" with others.

          Otherwise @SGaist's QtSingleApplication may be the way to go.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #6

          @JNBarchan said in Qt5 prevent multiple instances of application:

          The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another....

          i think the chances that this happens (on the same machine!) are very very small. ;)
          And (personally) i doubt that an application of a developer which doesn't see/know that it's supposed to be unique is spread like this :)

          Nevertheless of course you are basically right.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          JonBJ 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @JNBarchan said in Qt5 prevent multiple instances of application:

            The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another....

            i think the chances that this happens (on the same machine!) are very very small. ;)
            And (personally) i doubt that an application of a developer which doesn't see/know that it's supposed to be unique is spread like this :)

            Nevertheless of course you are basically right.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #7

            @raven-worx said in Qt5 prevent multiple instances of application:

            @JNBarchan said in Qt5 prevent multiple instances of application:

            The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another....

            i think the chances that this happens (on the same machine!) are very very small. ;)

            If I copy the code suggestion into my application, and the OP copies it into his application, the two applications cannot be run at the same time on any machine!

            raven-worxR 1 Reply Last reply
            0
            • JonBJ JonB

              @raven-worx said in Qt5 prevent multiple instances of application:

              @JNBarchan said in Qt5 prevent multiple instances of application:

              The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another....

              i think the chances that this happens (on the same machine!) are very very small. ;)

              If I copy the code suggestion into my application, and the OP copies it into his application, the two applications cannot be run at the same time on any machine!

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #8

              @JNBarchan
              so you think a shared memory is shared world-wide to any machine??? o.O
              are you really serious?!?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              JonBJ 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @JNBarchan
                so you think a shared memory is shared world-wide to any machine??? o.O
                are you really serious?!?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #9

                @raven-worx said in Qt5 prevent multiple instances of application:

                @JNBarchan
                so you think a shared memory is shared world-wide to any machine??? o.O
                are you really serious?!?

                Oh, I think we are misunderstanding. When I wrote

                the two applications cannot be run at the same time on any machine

                I meant, the two apps cannot both be run on the same machine anywhere! Not, each one on a different machine, obviously!

                The point is not really that the OP should not use that literal string --- though I'd really rather he did not --- it's just that if the post suggestion simply alerted the reader to the fact that the string should be unique across applications we would know where we are. Hence I drew attention to the fact. That's all.

                1 Reply Last reply
                0
                • Vinod KuntojiV Vinod Kuntoji

                  @alecs26 ,

                  Inside the main(), put this
                  QApplication a(argc, argv);

                  QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");
                  if( !shared.create( 512, QSharedMemory::ReadWrite) )
                  {
                  qWarning() << "Can't start more than one instance of the application.";
                  exit(0);
                  }
                  else {
                  qDebug() << "Application started successfully.";
                  }

                  B Offline
                  B Offline
                  Bharath Ram
                  wrote on last edited by
                  #10

                  @Vinod-Kuntoji Thank you very, very, very much.

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    hbatalha
                    wrote on last edited by hbatalha
                    #11

                    This is a very old post but I want to share my experience for newcomers.

                    I decided to use @Vinod-Kuntoji solution and it worked as expected. However, when testing the application on a linux environment I ran into a problem: if my application crashes or finishes unexpectedly, when trying to run it again I always received the message stating that the application is already running, mostly when rerunning the app from the Qt Creator that would kill the running app and run it again. It was pretty annoying, I had to change the ID string when that happened.

                    I got past the problem by doing this:

                    int main(int argc, char *argv[])
                    {
                    /*
                        QApplication a(argc, argv);
                    
                        QSharedMemory* shared = new QSharedMemory("62d60yyiassassasaasxss669-bb94-4a9gjhsfgjdhiuj4-8ihi8bb-b9648kjuiujhjhtgjdf90a7e04");
                    
                        shared->deleteLater(); // this solved the problem
                    
                        if( ! shared->create( 512, QSharedMemory::ReadWrite) )
                        {
                            QMessageBox errorMessage;
                    
                            QApplication::beep();
                    
                            errorMessage.addButton(QMessageBox::Ok);
                            errorMessage.setWindowTitle(QObject::tr("Error"));
                    
                            errorMessage.setText(QObject::tr("Already running"));
                            errorMessage.setInformativeText(QObject::tr("More than one instance of the program is not permitted"));
                    
                            errorMessage.exec();
                    
                            exit(0);
                        }
                        else
                        {
                            MainWindow w;
                    
                            w.show();
                            return a.exec();
                        }
                    */
                    }
                    JonBJ 1 Reply Last reply
                    0
                    • H hbatalha

                      This is a very old post but I want to share my experience for newcomers.

                      I decided to use @Vinod-Kuntoji solution and it worked as expected. However, when testing the application on a linux environment I ran into a problem: if my application crashes or finishes unexpectedly, when trying to run it again I always received the message stating that the application is already running, mostly when rerunning the app from the Qt Creator that would kill the running app and run it again. It was pretty annoying, I had to change the ID string when that happened.

                      I got past the problem by doing this:

                      int main(int argc, char *argv[])
                      {
                      /*
                          QApplication a(argc, argv);
                      
                          QSharedMemory* shared = new QSharedMemory("62d60yyiassassasaasxss669-bb94-4a9gjhsfgjdhiuj4-8ihi8bb-b9648kjuiujhjhtgjdf90a7e04");
                      
                          shared->deleteLater(); // this solved the problem
                      
                          if( ! shared->create( 512, QSharedMemory::ReadWrite) )
                          {
                              QMessageBox errorMessage;
                      
                              QApplication::beep();
                      
                              errorMessage.addButton(QMessageBox::Ok);
                              errorMessage.setWindowTitle(QObject::tr("Error"));
                      
                              errorMessage.setText(QObject::tr("Already running"));
                              errorMessage.setInformativeText(QObject::tr("More than one instance of the program is not permitted"));
                      
                              errorMessage.exec();
                      
                              exit(0);
                          }
                          else
                          {
                              MainWindow w;
                      
                              w.show();
                              return a.exec();
                          }
                      */
                      }
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #12

                      @hbatalha said in Qt5 prevent multiple instances of application:

                      shared->deleteLater(); // this solved the problem

                      Are you sure this did not solve the problem by releasing the shared memory during a.exec()? You verified another instance still doesn't run?

                      H 2 Replies Last reply
                      0
                      • JonBJ JonB

                        @hbatalha said in Qt5 prevent multiple instances of application:

                        shared->deleteLater(); // this solved the problem

                        Are you sure this did not solve the problem by releasing the shared memory during a.exec()? You verified another instance still doesn't run?

                        H Offline
                        H Offline
                        hbatalha
                        wrote on last edited by
                        #13

                        @JonB I tested one way only, it actually defeated the purpose.

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @hbatalha said in Qt5 prevent multiple instances of application:

                          shared->deleteLater(); // this solved the problem

                          Are you sure this did not solve the problem by releasing the shared memory during a.exec()? You verified another instance still doesn't run?

                          H Offline
                          H Offline
                          hbatalha
                          wrote on last edited by
                          #14

                          @JonB I believe this answer solves the problem I described above.

                          1 Reply Last reply
                          2

                          • Login

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