diff --git a/src/main.cpp b/src/main.cpp index 41a85703..3ea52267 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,14 +85,8 @@ int main(int argc, char *argv[]) QtSingleApplication a(id, argc, argv); qInstallMessageHandler(messageOutput); if (a.isRunning()) { - qInfo() << "Kylin-Network-Manager Is Already Launched, just show"; - auto connection = QDBusConnection::sessionBus(); - QDBusInterface iface("com.kylin.network", - "/com/kylin/network", - "com.kylin.network", - connection); - iface.call("showMainWindow"); - return 0; + a.sendMessage("raise_window_noop"); + return EXIT_SUCCESS; } QApplication::setQuitOnLastWindowClosed(false); @@ -116,6 +110,7 @@ int main(int argc, char *argv[]) } MainWindow w; + a.setActivationWindow(&w); w.setProperty("useStyleWindowManager", false); //禁用拖动 //设置窗口无边框,阴影 #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) diff --git a/src/singleapplication/qt-single-application.cpp b/src/singleapplication/qt-single-application.cpp index b06144df..b924430c 100644 --- a/src/singleapplication/qt-single-application.cpp +++ b/src/singleapplication/qt-single-application.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include "../mainwindow.h" @@ -142,10 +143,9 @@ void QtSingleApplication::sysInit(const QString &appId) { - actWin = 0; - peer = new QtLocalPeer(this, appId); -// connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&))); - connect(peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::messageReceived); + m_activateWindow = 0; + m_peer = new QtLocalPeer(this, appId); + connect(m_peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::messageReceived); } @@ -240,7 +240,7 @@ QtSingleApplication::QtSingleApplication(Display* dpy, const QString &appId, int */ bool QtSingleApplication::isRunning() { - return peer->isClient(); + return m_peer->isClient(); } @@ -258,7 +258,7 @@ bool QtSingleApplication::isRunning() { \sa isRunning(), messageReceived() */ bool QtSingleApplication::sendMessage(const QString &message, int timeout) { - return peer->sendMessage(message, timeout); + return m_peer->sendMessage(message, timeout); } @@ -267,7 +267,7 @@ bool QtSingleApplication::sendMessage(const QString &message, int timeout) { identifier will be regarded as instances of the same application. */ QString QtSingleApplication::id() const { - return peer->applicationId(); + return m_peer->applicationId(); } @@ -284,12 +284,11 @@ QString QtSingleApplication::id() const { */ void QtSingleApplication::setActivationWindow(QWidget* aw, bool activateOnMessage) { - actWin = aw; - //目前不需要用到此处的置顶方法,故此信号槽暂时注释掉,若后续需要根据新起进程传递的信号执行部分操作时可以把这里放开 -// if (activateOnMessage) -// connect(peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); -// else -// disconnect(peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); + m_activateWindow = aw; + if (activateOnMessage) + connect(m_peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); + else + disconnect(m_peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); } @@ -300,7 +299,7 @@ void QtSingleApplication::setActivationWindow(QWidget* aw, bool activateOnMessag \sa setActivationWindow() */ QWidget* QtSingleApplication::activationWindow() const { - return actWin; + return m_activateWindow; } @@ -319,24 +318,21 @@ QWidget* QtSingleApplication::activationWindow() const { \sa setActivationWindow(), messageReceived(), initialize() */ void QtSingleApplication::activateWindow() { - //单例置顶策略,由于bootOptionsFilter in mainwindow自带置顶策略,故注掉此处 -// if (actWin) { -// if(this->applicationState() & Qt::ApplicationInactive) -// { -// MainWindow* w=qobject_cast(actWin); -//// w->loadMainWindow(); -// w->clearSearchResult(); -// actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized); -// actWin->raise(); -// actWin->showNormal(); -// actWin->activateWindow(); -// } -// else { -// actWin->setWindowState(actWin->windowState() & Qt::WindowMinimized); -// actWin->hide(); -// } - -// } + if (m_activateWindow) { + if(this->applicationState() & Qt::ApplicationInactive) + { + MainWindow* w=qobject_cast(m_activateWindow); + w->showMainwindow(); + m_activateWindow->setWindowState(m_activateWindow->windowState() & ~Qt::WindowMinimized); + m_activateWindow->raise(); + m_activateWindow->showNormal(); + m_activateWindow->activateWindow(); + } + else { + m_activateWindow->setWindowState(m_activateWindow->windowState() & Qt::WindowMinimized); + m_activateWindow->hide(); + } + } } diff --git a/src/singleapplication/qt-single-application.h b/src/singleapplication/qt-single-application.h index 15df5a8b..084b9b7c 100644 --- a/src/singleapplication/qt-single-application.h +++ b/src/singleapplication/qt-single-application.h @@ -102,8 +102,8 @@ Q_SIGNALS: private: void sysInit(const QString &appId = QString()); - QtLocalPeer *peer; - QWidget *actWin; + QtLocalPeer *m_peer; + QWidget *m_activateWindow; }; #endif // QTSINGLEAPPLICATION_H