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. How can i prevent my app open multiple times with Qt?
Forum Updated to NodeBB v4.3 + New Features

How can i prevent my app open multiple times with Qt?

Scheduled Pinned Locked Moved General and Desktop
33 Posts 7 Posters 14.7k Views 1 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #13

    Ok so here is the complete outpout of ./configure -> qmake -> make ( http://paste.ubuntu.com/688207/ )
    After doing that i am including QtSingleApplication at my app and it says QtSingleApplication is not declared.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #14

      Build looks fine. Have you set up include path correctly? Have you even included the neccessary header files?

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #15

        So you mean copying the qtsinglecoreapplication.h file at my project directory and then
        @#include "qtsingleapplication.h"
        @
        ?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #16

          The usual way is to add the path to the include path in your project file.
          @
          INCLUDEPATH += <location_of_qtsingleapplication_headers>
          @
          In addition, the QtSingleApplication libraries have to be added too.
          @
          LIBS += -L<location_of_qtsingleapplication_libraries>
          LIBS += -l<name_of_qtsingleapplication_library>
          @
          And - of course - you will have to include the headers in your sources files too.
          @
          #include <QtSingleApplication>
          @

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Leon
            wrote on last edited by
            #17

            So whats wrong with that:
            @INCLUDEPATH += <~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src>
            LIBS += -L<~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src>
            LIBS += -l<~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src/qtsinglecoreapplication.h>@
            ?

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vsorokin
              wrote on last edited by
              #18

              Remove < and > simbols

              --
              Vasiliy

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Leon
                wrote on last edited by
                #19

                Forgot.
                @INCLUDEPATH += ~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src
                LIBS += -L~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src
                LIBS += -l~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src/qtsinglecoreapplication.h@

                But still i cant
                @#include <QtSingleApplication>@
                at my cpp file.

                P.S Should it have a spece between -l and ~ symbol.?

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  vsorokin
                  wrote on last edited by
                  #20

                  Try this

                  @INCLUDEPATH += ~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src
                  LIBS += -L~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src
                  LIBS += -lqtsinglecoreapplication
                  @

                  --
                  Vasiliy

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Leon
                    wrote on last edited by
                    #21

                    The same. I can only include the "qtsingleapplication.h".

                    P.S I have reopened qt so as to refresh.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #22

                      There are two ways on how to use QtSingleApplication:

                      • You add the sources directly to your project or
                      • You compile QtSingleApplication into a library and your application links against this library

                      #1 requires you to add all of the QtSingleApplication sources to your application. The easiest way to do is including the qtsingleapplication.pri from the src directory.
                      @
                      // .pro
                      include(../qtsingleapplication/src/qtsingleapplication.pri)
                      @

                      #2 requires you to build the library first
                      @
                      // shell
                      ./configure -library
                      qmake
                      make
                      @
                      and then reference it in your project file
                      @
                      // .pro
                      INCLUDEPATH += ../qtsingleapplication/src
                      LIBS += -L../qtsingleapplication/lib
                      LIBS += -lQtSolutions_SingleApplication-head
                      @

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        Leon
                        wrote on last edited by
                        #23

                        How should it be with the first way?
                        @include(~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src/qtsingleapplication.pri)
                        @
                        ?

                        P.S All this sounds a bit complicated, and if someone wants to compile my app they must download qtsingleapplication librarie. Should i stay with the command pidof or QtSingleApplication will be better?

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lgeyer
                          wrote on last edited by
                          #24

                          It is as complicated as integrating any other third party code or library. QtSingleApplications license allows you to redistribute it along with your code. So if you have set it up properly there is no extra work for "someone".

                          I would go with QtSingleApplication. It is much less error-prone than the pidof approach.

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            Leon
                            wrote on last edited by
                            #25

                            I can't get it.
                            So if you have set it up properly there is no extra work for “someone”.

                            With both ways you must set the path to the qtsingleapplication library. How someone that doesnt have it won't need to do extra work?

                            P.S You didn't answer yo my previous question. With:
                            @include(~/Downloads/qt-solutions-qt-solutions/qtsingleapplication/src/qtsingleapplication.pri)@
                            all still the same.

                            Appreciate any help until now :)

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lgeyer
                              wrote on last edited by
                              #26

                              Well, I do not consider adding a single line or three lines to a file extra work ;-)

                              And if you redistribute the QtSingleApplication code along with your application, then no, there isn't even this "work" for someone because you have done all the "work" for them.

                              I'm not quite sure if qmake correctly expands the ~. Use an absolute path or a path relative to your project instead. Take a look at the command line which is executed when make is run - are the include pathes correctly set there (-I <path>)?

                              1 Reply Last reply
                              0
                              • L Offline
                                L Offline
                                Leon
                                wrote on last edited by
                                #27

                                i copied the src folder at my application folder and added
                                @include(src/qtsingleapplication.pri)@
                                to my .pro file.. Just the same. I think i will go with pidof..

                                1. src folder is 50 kb ( my app's is 100kb so it's kinda big size )
                                2. i just can't implement QtSingleApplication at my app whatever i do
                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  dmcr
                                  wrote on last edited by
                                  #28

                                  Another simple way is to use QSharedMemory :

                                  @int main(int argc, char *argv[])
                                  {
                                  QApplication app(argc, argv);

                                  // Unique Application
                                  QSharedMemory sharedMemory("{--KeyCode--}");
                                  if(!sharedMemory.create(sizeof(int)))
                                  {
                                      qDebug()<< "already running!" ;
                                      return 1;
                                  }
                                  else
                                  {
                                      new Start();
                                      return app.exec&#40;&#41;;
                                  }
                                  

                                  }@

                                  dmcr

                                  1 Reply Last reply
                                  0
                                  • L Offline
                                    L Offline
                                    Leon
                                    wrote on last edited by
                                    #29

                                    [quote author="dmcr" date="1315982933"]Another simple way is to use QSharedMemory :

                                    @int main(int argc, char *argv[])
                                    {
                                    QApplication app(argc, argv);

                                    // Unique Application
                                    QSharedMemory sharedMemory("{--KeyCode--}");
                                    if(!sharedMemory.create(sizeof(int)))
                                    {
                                        qDebug()<< "already running!" ;
                                        return 1;
                                    }
                                    else
                                    {
                                        new Start();
                                        return app.exec&#40;&#41;;
                                    }
                                    

                                    }@[/quote]

                                    This one works fine :D
                                    But why i can't get it focus at the window when the app is running?
                                    None of this works:

                                    MainWindow w;
                                    w.activateWindow();
                                    w.raise();
                                    w.setFocus();
                                    w.setVisible(1);@

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      giesbert
                                      wrote on last edited by
                                      #30

                                      Hehe, yes, this is a problem.

                                      I'm not 100% sure about the linux way, but on windows it is not possible to pull the focus from a different app. And you app does not have the focus. It is on the desktop or who ever opens the process. the new process would get the focus, but it emideatly terminates, so --> it does not work ;-(

                                      Nokia Certified Qt Specialist.
                                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        Robbin
                                        wrote on last edited by
                                        #31

                                        Leon, I had troubles with that for some time as well. One of the features about QtSingleApplication was that I could communicate with the running copy and send (eventually) commands to it (with QTcpServer haven't really tested with shared memory as there was something in the documents I was looking at against using that). All that ended up that I could actually receive commands on windows only, but I could still detect the app is already running and stop the new instance. And yeah, as Gerolf said - you cannot take the focus of other application. Tried all kind of ways, as you did, the best you can do is to show a notification, make the taskbar entry "flashing" =/

                                        I guess you could always do that in some native linux way.

                                        1 Reply Last reply
                                        0
                                        • L Offline
                                          L Offline
                                          Leon
                                          wrote on last edited by
                                          #32

                                          [quote author="Eus" date="1316069611"]Leon, I had troubles with that for some time as well. One of the features about QtSingleApplication was that I could communicate with the running copy and send (eventually) commands to it (with QTcpServer haven't really tested with shared memory as there was something in the documents I was looking at against using that). All that ended up that I could actually receive commands on windows only, but I could still detect the app is already running and stop the new instance. And yeah, as Gerolf said - you cannot take the focus of other application. Tried all kind of ways, as you did, the best you can do is to show a notification, make the taskbar entry "flashing" =/

                                          I guess you could always do that in some native linux way.[/quote]

                                          My friend's app is focusing, using pidof to test if the application is already running. I will post back when he tells me the way to do the focusing thing.

                                          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