Open some file with running instance of a QtApplication
-
wrote on 21 Jul 2022, 09:40 last edited by
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
-
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
wrote on 21 Jul 2022, 09:55 last edited by@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. -
wrote on 21 Jul 2022, 14:50 last edited by
Thanks for the quick answer. I'll check out if that can solve my problem.
-
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.
-
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
@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.
-
@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.
@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-Hilk if memory serves well, on some OS, QSharedMemory can leave leftovers if the application crashes and needs cleanup before it can be used.
@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.
1/7