From c385edb7f7babef4d46ca77f1a979dd9f139b16e Mon Sep 17 00:00:00 2001
From: luoqing <luoqing@kylinsec.com.cn>
Date: Thu, 21 Sep 2023 10:33:42 +0800
Subject: [PATCH 2/2] fix(network-tray):Fixed an issue where the size Settings
 of widgets that are not currently displaying pages in the stackwidget did not
 take effect when multiple nics were present during tray initialization
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 修复在托盘初始化时，存在多个网卡时，stackwidget中当前未显示页面的widget的设置大小未生效的问题

Close #13862
---
 plugins/network/src/tray/tray-page.cpp | 35 ++++++++++++++++++++++++++
 plugins/network/src/tray/tray-page.h   |  3 +++
 2 files changed, 38 insertions(+)

diff --git a/plugins/network/src/tray/tray-page.cpp b/plugins/network/src/tray/tray-page.cpp
index 753abc4..2e50f22 100644
--- a/plugins/network/src/tray/tray-page.cpp
+++ b/plugins/network/src/tray/tray-page.cpp
@@ -53,6 +53,41 @@ void TrayPage::initUI()
     {
         setSingleDeviceWidget();
     }
+
+    /**
+     * NOTE:
+     * 此处是修复当存在多个网卡时，stackwidget中当前未显示页面的widget的大小有问题
+     * 推测原因在于，在初始化时，在页面未show的状态下，设置widget的尺寸不生效存在问题。
+    */
+    repolish(this);
+}
+
+void TrayPage::repolish(QWidget *w)
+{
+    QList<const QObject *> children;
+    children.reserve(w->children().size() + 1);
+    for (auto child: qAsConst(w->children()))
+        children.append(child);
+    children.append(w);
+    updateObjects(children);
+}
+
+void TrayPage::updateObjects(const QList<const QObject *>& objects)
+{
+    QEvent event(QEvent::StyleChange);
+    for (const QObject *object : objects) 
+    {
+        if (auto widget = qobject_cast<QWidget*>(const_cast<QObject*>(object))) 
+        {
+            widget->style()->polish(widget);
+            QCoreApplication::sendEvent(widget, &event);
+            QList<const QObject *> children;
+            children.reserve(widget->children().size() + 1);
+            for (auto child: qAsConst(widget->children()))
+                children.append(child);
+            updateObjects(children);
+        }
+    }
 }
 
 void TrayPage::initConnection()
diff --git a/plugins/network/src/tray/tray-page.h b/plugins/network/src/tray/tray-page.h
index 28948a4..66a84f2 100644
--- a/plugins/network/src/tray/tray-page.h
+++ b/plugins/network/src/tray/tray-page.h
@@ -44,6 +44,9 @@ public:
 
     QSize trayPageSize();
 
+    void repolish(QWidget *w);
+    void updateObjects(const QList<const QObject *>& objects);
+
 public slots:
     void handleDeviceComboBoxChanged(int index);
     void handleAdjustedTraySize(QSize sizeHint);
-- 
2.33.0

