From 21869cc2e8c695db1bf048379b19c11cdd91c55f Mon Sep 17 00:00:00 2001 From: zhangyuanyuan1 Date: Fri, 6 Jan 2023 15:22:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3pc=E6=A8=A1=E5=BC=8F=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=A0=8F=E6=97=A0=E7=BD=91=E7=BB=9C=E5=9B=BE=E6=A0=87?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/mainwindow.cpp | 30 ++++++++++++++++++++++++++---- src/frontend/mainwindow.h | 3 +++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/frontend/mainwindow.cpp b/src/frontend/mainwindow.cpp index 496d26b2..767c909d 100644 --- a/src/frontend/mainwindow.cpp +++ b/src/frontend/mainwindow.cpp @@ -115,6 +115,7 @@ void MainWindow::firstlyStart() { initWindowProperties(); initTransparency(); + registerTrayIcon(); initUI(); initDbusConnnect(); initWindowTheme(); @@ -265,6 +266,30 @@ void MainWindow::initUI() paintWithTrans(); } +/** + * @brief MainWindow::registerTrayIcon 注册托盘图标 + */ +void MainWindow::registerTrayIcon() +{ + m_registerCount++; + if (QSystemTrayIcon::isSystemTrayAvailable() || m_registerCount > 10) { + m_trayIcon = new QSystemTrayIcon(); + if (nullptr == m_trayIcon) { + qWarning()<< "分配空间trayIcon失败"; + return ; + } + m_trayIcon->setIcon(QIcon::fromTheme("network-wired-signal-excellent-symbolic")); + m_trayIcon->setToolTip(QString(tr("kylin-nm"))); + + } else { + if (m_registerCount <= 10) { + QTimer::singleShot(m_intervalTime,[this] { + registerTrayIcon(); + }); + } + } +} + /** * @brief MainWindow::initTrayIcon 初始化托盘图标和托盘右键菜单 */ @@ -285,17 +310,14 @@ void MainWindow::initTrayIcon() iconTimer = new QTimer(this); connect(iconTimer, &QTimer::timeout, this, &MainWindow::onSetTrayIconLoading); - m_trayIcon = new QSystemTrayIcon(); m_trayIconMenu = new QMenu(); m_showMainwindowAction = new QAction(tr("Show MainWindow"),this); m_showSettingsAction = new QAction(tr("Settings"),this); - - m_trayIcon->setToolTip(QString(tr("kylin-nm"))); m_showSettingsAction->setIcon(QIcon::fromTheme("document-page-setup-symbolic", QIcon(":/res/x/setup.png")) ); // m_trayIconMenu->addAction(m_showMainwindowAction); m_trayIconMenu->addAction(m_showSettingsAction); + m_trayIcon->setContextMenu(m_trayIconMenu); - m_trayIcon->setIcon(QIcon::fromTheme("network-wired-signal-excellent-symbolic")); iconStatus = IconActiveType::LAN_CONNECTED; onRefreshTrayIcon(); diff --git a/src/frontend/mainwindow.h b/src/frontend/mainwindow.h index 3eea80ec..8917d3bf 100644 --- a/src/frontend/mainwindow.h +++ b/src/frontend/mainwindow.h @@ -124,6 +124,7 @@ private: void paintWithTrans(); void initUI(); void initDbusConnnect(); + void registerTrayIcon(); void initTrayIcon(); void resetTrayIconTool(); void initWindowTheme(); @@ -157,6 +158,8 @@ private: QMenu * m_trayIconMenu = nullptr; QAction * m_showMainwindowAction = nullptr; QAction * m_showSettingsAction = nullptr; + uint m_intervalTime = 100; + uint m_registerCount = 0; bool m_lanIsLoading = false; bool m_wlanIsLoading = false;