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 486 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on 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

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • gde23G gde23

      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

      JonBJ Online
      JonBJ Online
      JonB
      wrote on 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
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          • gde23G gde23

            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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on 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.

            SGaistS 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

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

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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.HilkJ 1 Reply Last reply
              0
              • SGaistS SGaist

                @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.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on 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

                • Login

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