Single instance application in Qt
For single instance, read Run only one instance of a Qt application.
Disregarding portability, if you are running on Windows, another common approach is to create a named pipe (or a named mutex) when the application starts, and destroy it before exiting. If the named pipe already exists, another instance is already running. You could even write to the named pipe asking the other instance to bring its window to front, altough a console application might not benefit from this specifically.
Another way to focus the existing instance is to find the window of the already running instance, then call SetFocus on its HWND
, or the corresponding function on your platform.
How to manage single instance of a Qt app on crash?
You can use a QSetting:
int main(int argc, char *argv[])
{
QSettings settings;
QApplication a(argc, argv);
if(!settings.exitedNormaly()) {
// In case of crash
}
// set the flag to false
settings.setExitedNormaly(false);
MainWindow w(&settings);
w.processArg(argc, argv);
w.show();
int result = a.exec();
settings.setExitedNormaly(result == 0);
return result;
}
Combined with your shared memory, you'll be able to unlock the block in case of application crash.
Qt5: Preventing another instance of the application doesn't work anymore...!
I finally discovered where the problem was... and it's not the antivirus to be blamed :)
When I upgraded the Qt (Creator v3.6.0) to the newest version (5.5.1), there is a setting in Tools->Options->Build&Run named [Stop app before building]
... that was set to Current project
or something. Hence, the Qt Creator was killing the old instance before launching a new one(!).
Setting this option to None
solved the issue.
So, it seems the code was just fine, antivirus was fine, yet launching the app from within Qt Creator was somehow restricted to only one instance :)
I decided to share this, maybe it will be helpful for other folks as well.
Remark : I checked again and now I can confirm: That setting didn't exist before, at least not in Qt Creator v3.3.2.
Restoring or bringing to front Qt desktop application
For some reason setActivationWindow()
and activateWindow()
don't work for me. This is my workaround:
#include <QWidget>
#include <qtsingleapplication.h>
class Window : public QWidget
{
Q_OBJECT
public:
Window(QWidget *parent = 0) : QWidget(parent) {}
public slots:
void readMessage(const QString &str) { showNormal(); }
};
int main(int argc, char *argv[])
{
QtSingleApplication instance(argc, argv);
Window *window = new Window;
window->show();
QObject::connect(&instance, SIGNAL(messageReceived(const QString &)), window, SLOT(readMessage(const QString &)));
if (instance.sendMessage(""))
return 0;
return instance.exec();
}
#include "main.moc"
Find QWidget of single instance Qt application
You should use the qtsingleapplication API
edit- It's a separate download see here for both LGPL and Commercial editions
Best practice when setting a string to (possibly) its own value?
It doesn't matter in these cases. Pick what looks clearest to you and your team. Don't sweat over it.
There's no general rule of thumb either. There are specific cases where checking before setting is a must because of some side effects in the setter and there are specific cases where settings regardless is a must because of performance criticality / checking is expensive / multithreading, this case is neither.
Related Topics
Lifetime of a String Literal Returned by a Function
Is the Pointer Guaranteed to Preserve Its Value After 'Delete' in C++
C++ Exception:Throwing Std::String
Std::Fstream Buffering VS Manual Buffering (Why 10X Gain with Manual Buffering)
Is It Legal to Redefine a C++ Keyword
Simple Way to Unzip a .Zip File Using Zlib
How to Emit Cross-Thread Signal in Qt
When Is It Necessary to Use the Flag -Stdlib=Libstdc++
Why Isn't the [] Operator Const for Stl Maps
Stdafx + Header File - Order of Inclusion in Mfc Application
Outputting Date and Time in C++ Using Std::Chrono
How to Remove All the Occurrences of a Char in C++ String
Seeking and Reading Large Files in a Linux C++ Application
Check Traits for All Variadic Template Arguments