From 87375e43fc8b614810093d4558a146beb4926f5b Mon Sep 17 00:00:00 2001 From: fensite Date: Fri, 26 Nov 2021 21:05:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?add=20function=20of=20calling=20up=20the=20?= =?UTF-8?q?mainWindow=20from=20the=20command=20line=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C=E6=8B=89=E8=B5=B7=E5=8D=95=E4=BE=8B?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E4=B8=BB=E7=AA=97=E5=8F=A3=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20BugId:=2090875?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 11 ++--- .../qt-single-application.cpp | 41 +++++++++---------- 2 files changed, 23 insertions(+), 29 deletions(-) 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..39dd99c3 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" @@ -286,10 +287,10 @@ 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); + if (activateOnMessage) + connect(peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); + else + disconnect(peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); } @@ -320,23 +321,21 @@ QWidget* QtSingleApplication::activationWindow() const { */ 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 (actWin) { + if(this->applicationState() & Qt::ApplicationInactive) + { + MainWindow* w=qobject_cast(actWin); + w->showMainwindow(); + actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized); + actWin->raise(); + actWin->showNormal(); + actWin->activateWindow(); + } + else { + actWin->setWindowState(actWin->windowState() & Qt::WindowMinimized); + actWin->hide(); + } + } } From faccb69ae790486a645d7fe38013f8a2b26c37ca Mon Sep 17 00:00:00 2001 From: fensite Date: Mon, 29 Nov 2021 10:13:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?delete=20redundant=20comments=20and=20modif?= =?UTF-8?q?y=20the=20format=20of=20qtsingleapplication=20class=20member=20?= =?UTF-8?q?variables=20=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99=E7=9A=84?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=EF=BC=8C=E4=BF=AE=E6=94=B9QtSingleApplicatio?= =?UTF-8?q?n=E7=B1=BB=E6=88=90=E5=91=98=E5=8F=98=E9=87=8F=E7=9A=84?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qt-single-application.cpp | 39 +++++++++---------- src/singleapplication/qt-single-application.h | 4 +- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/singleapplication/qt-single-application.cpp b/src/singleapplication/qt-single-application.cpp index 39dd99c3..b924430c 100644 --- a/src/singleapplication/qt-single-application.cpp +++ b/src/singleapplication/qt-single-application.cpp @@ -143,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); } @@ -241,7 +240,7 @@ QtSingleApplication::QtSingleApplication(Display* dpy, const QString &appId, int */ bool QtSingleApplication::isRunning() { - return peer->isClient(); + return m_peer->isClient(); } @@ -259,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); } @@ -268,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(); } @@ -285,12 +284,11 @@ QString QtSingleApplication::id() const { */ void QtSingleApplication::setActivationWindow(QWidget* aw, bool activateOnMessage) { - actWin = aw; - //目前不需要用到此处的置顶方法,故此信号槽暂时注释掉,若后续需要根据新起进程传递的信号执行部分操作时可以把这里放开 + m_activateWindow = aw; if (activateOnMessage) - connect(peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); + connect(m_peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); else - disconnect(peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); + disconnect(m_peer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow); } @@ -301,7 +299,7 @@ void QtSingleApplication::setActivationWindow(QWidget* aw, bool activateOnMessag \sa setActivationWindow() */ QWidget* QtSingleApplication::activationWindow() const { - return actWin; + return m_activateWindow; } @@ -320,20 +318,19 @@ QWidget* QtSingleApplication::activationWindow() const { \sa setActivationWindow(), messageReceived(), initialize() */ void QtSingleApplication::activateWindow() { - //单例置顶策略,由于bootOptionsFilter in mainwindow自带置顶策略,故注掉此处 - if (actWin) { + if (m_activateWindow) { if(this->applicationState() & Qt::ApplicationInactive) { - MainWindow* w=qobject_cast(actWin); + MainWindow* w=qobject_cast(m_activateWindow); w->showMainwindow(); - actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized); - actWin->raise(); - actWin->showNormal(); - actWin->activateWindow(); + m_activateWindow->setWindowState(m_activateWindow->windowState() & ~Qt::WindowMinimized); + m_activateWindow->raise(); + m_activateWindow->showNormal(); + m_activateWindow->activateWindow(); } else { - actWin->setWindowState(actWin->windowState() & Qt::WindowMinimized); - actWin->hide(); + 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