1

I have made my app into a single instance app using the RunGuard code found on this SO question:

Qt: Best practice for a single instance app protection

What I'd like to do is when the user tries to start the application while there is one running is to bring the existing running application to the front, and if minimised, restore it.

In my Delphi Windows programming days I used to broadcast a Windows message from the new application before closing it. The existing app would receive this and restore itself and come to the front.

Is something like this possible with Qt on Windows and Linux platforms?

Community
  • 1
  • 1
Michael Vincent
  • 1,620
  • 1
  • 20
  • 46

3 Answers3

2

Did you have any specific trouble with QtSingleApplication? It should be sufficient for what you want and will enable you to send a message to the running application. You just need a slot to get that message and if it matches what you expect then you restore it.

http://doc.qt.digia.com/solutions/4/qtsingleapplication/qtsingleapplication-example-trivial.html

The logview object is also set as the application's activation window. Every time a message is received, the window will be raised and activated automatically.

fleed
  • 640
  • 9
  • 15
1

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"
svlasov
  • 9,923
  • 2
  • 38
  • 39
1

In common, it is not possible without IPC. QtSingleApplication provide such IPC, but you will get extra dependency from QtNetwork module. (As @svlasov answered)

First problem that you will have: you can't raise any window of application if this application is not foreground. There are solutions for Windows and OS X, how to force raising of windows.

Community
  • 1
  • 1
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61