#include "qtsingleapplication.h"
int main(int argc, char *argv[])
{
SharedTools::QtSingleApplication a("123", argc, argv);
if (a.isRunning())
{
a.sendMessage("message from other instance.");
return 0;
}
widget w;
//唯一性信号槽连接:接收到已在运行发出的信息后,发送信号messageReceived()
QObject::connect(&a, SIGNAL(messageReceived(const QString&,QObject *)), &w, SLOT(soltMessageReceived()));
w.show();
return a.exec();
}
void widget::soltMessageReceived()
{
this->setWindowState((this->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
this->setFocus(Qt::ActiveWindowFocusReason);
QApplication::setActiveWindow(this);
}