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. Open some file with running instance of a QtApplication
Forum Updated to NodeBB v4.3 + New Features

Open some file with running instance of a QtApplication

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 479 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.
  • G Offline
    G Offline
    gde23
    wrote on 21 Jul 2022, 09:40 last edited by
    #1

    Hello,

    not sure if this is a Qt-related problem or has to do with the Os.

    I've a file that stores data for my application.
    Now when I open one of the files, it will always start a new instance of my application.
    However what I want is, that it opens within the running instance when there is one, e.g. in a new tab.
    How can I achieve such behavior?

    Thanks

    J J 2 Replies Last reply 21 Jul 2022, 09:55
    0
    • G gde23
      21 Jul 2022, 09:40

      Hello,

      not sure if this is a Qt-related problem or has to do with the Os.

      I've a file that stores data for my application.
      Now when I open one of the files, it will always start a new instance of my application.
      However what I want is, that it opens within the running instance when there is one, e.g. in a new tab.
      How can I achieve such behavior?

      Thanks

      J Online
      J Online
      JonB
      wrote on 21 Jul 2022, 09:55 last edited by
      #2

      @gde23
      You need to Google "qt application single instance" to make it so only one instance of application can be opened.
      I do not know how it then gets that you have double-clicked another file while it is open, might be windowing system specific.

      1 Reply Last reply
      1
      • G Offline
        G Offline
        gde23
        wrote on 21 Jul 2022, 14:50 last edited by
        #3

        Thanks for the quick answer. I'll check out if that can solve my problem.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 21 Jul 2022, 18:19 last edited by
          #4

          Hi,

          There is the QtSingleApplication from the old Qt Solution module that can fill the bill.

          You might be able to find more recent version though.

          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
          1
          • G gde23
            21 Jul 2022, 09:40

            Hello,

            not sure if this is a Qt-related problem or has to do with the Os.

            I've a file that stores data for my application.
            Now when I open one of the files, it will always start a new instance of my application.
            However what I want is, that it opens within the running instance when there is one, e.g. in a new tab.
            How can I achieve such behavior?

            Thanks

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 22 Jul 2022, 06:34 last edited by
            #5

            @gde23 What comes to my mind would be using QSharedMemory

            I use that to enforce a single app instance for my application:

            const QString sharedMemory  = QStringLiteral("%1SingleInstance").arg(ApplicationName);
                QSharedMemory _singular(sharedMemory);
            
                    if(_singular.attach(QSharedMemory::ReadOnly)){
                        _singular.detach();
                        auto view = QQmlApplicationEngine() ;
                        view.load(QUrl("qrc:/MessageAppInstanceWarning.qml"));
                        QObject::connect(view.rootObjects().first(), SIGNAL(closeApplication(void)), qApp, SLOT(quit(void)));
                        a.exec();
                        return -42;
                    }else{
                        _singular.create(1);
                    }
            

            I'm pretty sure, you could use QSharedMemory also to pass the path - which should be part of command line arguments of the "2nd instance" - to the original AppInstance.

            And react accordingly.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            S 1 Reply Last reply 22 Jul 2022, 21:28
            0
            • J J.Hilk
              22 Jul 2022, 06:34

              @gde23 What comes to my mind would be using QSharedMemory

              I use that to enforce a single app instance for my application:

              const QString sharedMemory  = QStringLiteral("%1SingleInstance").arg(ApplicationName);
                  QSharedMemory _singular(sharedMemory);
              
                      if(_singular.attach(QSharedMemory::ReadOnly)){
                          _singular.detach();
                          auto view = QQmlApplicationEngine() ;
                          view.load(QUrl("qrc:/MessageAppInstanceWarning.qml"));
                          QObject::connect(view.rootObjects().first(), SIGNAL(closeApplication(void)), qApp, SLOT(quit(void)));
                          a.exec();
                          return -42;
                      }else{
                          _singular.create(1);
                      }
              

              I'm pretty sure, you could use QSharedMemory also to pass the path - which should be part of command line arguments of the "2nd instance" - to the original AppInstance.

              And react accordingly.

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 22 Jul 2022, 21:28 last edited by
              #6

              @J-Hilk if memory serves well, on some OS, QSharedMemory can leave leftovers if the application crashes and needs cleanup before it can be used.

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

              J 1 Reply Last reply 25 Jul 2022, 05:41
              0
              • S SGaist
                22 Jul 2022, 21:28

                @J-Hilk if memory serves well, on some OS, QSharedMemory can leave leftovers if the application crashes and needs cleanup before it can be used.

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 25 Jul 2022, 05:41 last edited by
                #7

                @SGaist that is true, because on unix QSharedMemory "owns" the shared memory segment, or rather your application "owns" that memory. And the destructor has to run, for it to be freed.

                Thats not too much of a problem, the next time your application starts, it will detect that memory segment is in use and will close itself -> running the destructor of the last QSharedMemory using that segment -> freeing it for the next run.

                One could automate that, if crashes are a concern.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0

                1/7

                21 Jul 2022, 09:40

                • Login

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