diff --git a/plugins/application-tray/ddeindicatortrayprotocol.cpp b/plugins/application-tray/ddeindicatortrayprotocol.cpp index 2ac086350..a64f0f448 100644 --- a/plugins/application-tray/ddeindicatortrayprotocol.cpp +++ b/plugins/application-tray/ddeindicatortrayprotocol.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2024-2026 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -178,7 +178,7 @@ void DDEindicatorProtocolHandlerPrivate::updateContent() Q_Q(DDEindicatorProtocolHandler); if (!enabled) return; - auto widget = static_cast(q->parent()); + auto widget = q->window(); if (widget) { widget->update(); @@ -333,9 +333,12 @@ bool DDEindicatorProtocolHandler::eventFilter(QObject *watched, QEvent *event) { Q_D(DDEindicatorProtocolHandler); - if (watched == parent()) { + if (watched == window()) { if (event->type() == QEvent::Paint) { - auto widget = static_cast(parent()); + auto widget = window(); + if (!widget) + return false; + QPainter painter(widget); QFontMetrics qfm = QFontMetrics(widget->font()); QRect tightTextRect = qfm.tightBoundingRect(d->m_text); diff --git a/plugins/application-tray/sniprotocolhandler.cpp b/plugins/application-tray/sniprotocolhandler.cpp index 4f85e7cd4..94a0ecbbf 100644 --- a/plugins/application-tray/sniprotocolhandler.cpp +++ b/plugins/application-tray/sniprotocolhandler.cpp @@ -328,8 +328,11 @@ bool SniTrayProtocolHandler::eventFilter(QObject *watched, QEvent *event) menu->setFixedSize(menu->sizeHint()); menu->winId(); - auto widget = static_cast(parent()); - auto plugin = Plugin::EmbedPlugin::get(widget->window()->windowHandle()); + auto *win = window()->windowHandle(); + if (!win) + return false; + + auto plugin = Plugin::EmbedPlugin::get(win); auto geometry = plugin->pluginPos(); auto pluginPopup = Plugin::PluginPopup::get(menu->windowHandle()); pluginPopup->setPluginId("application-tray"); diff --git a/plugins/application-tray/trayplugin.cpp b/plugins/application-tray/trayplugin.cpp index f1afcc73a..33783f4b5 100644 --- a/plugins/application-tray/trayplugin.cpp +++ b/plugins/application-tray/trayplugin.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2024-2026 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -83,14 +83,18 @@ void TrayPlugin::onTrayhandlerCreatd(QPointer handl m_widget.insert(id, new TrayWidget(handler)); auto remove = [this, id]() { + // 清理 tooltip 指针,因为 tooltip widget 是 handler 的成员变量, + // 当 handler 被销毁时会被删除,所以需要先从 map 中移除悬空指针 + m_tooltip.remove(id); + m_proyInter->itemRemoved(this, id); + auto widget = m_widget.value(id); if (widget) { widget->deleteLater(); } m_widget.remove(id); - m_tooltip.remove(id); }; auto showWidget = [this, handler, id](){ diff --git a/plugins/application-tray/traywidget.cpp b/plugins/application-tray/traywidget.cpp index 0f94d7225..f61cf04a8 100644 --- a/plugins/application-tray/traywidget.cpp +++ b/plugins/application-tray/traywidget.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -28,7 +28,8 @@ TrayWidget::TrayWidget(QPointer handler) setWindowTitle(m_handler->id()); setFixedSize(trayIconSize, trayIconSize); - m_handler->setParent(this); + // Note: Do NOT set parent here - handler lifecycle is managed by QSharedPointer + // Setting parent would cause double-delete when QSharedPointer destroys the handler connect(m_handler, &AbstractTrayProtocolHandler::iconChanged, this, [this](){update();}); connect(m_handler, &AbstractTrayProtocolHandler::overlayIconChanged, this, [this](){update();}); @@ -47,6 +48,9 @@ TrayWidget::~TrayWidget() void TrayWidget::showEvent(QShowEvent* event) { Q_UNUSED(event) + if (!m_handler) + return; + m_handler->setWindow(window()); window()->installEventFilter(m_handler); window()->setMouseTracking(true); @@ -56,6 +60,9 @@ void TrayWidget::paintEvent(QPaintEvent* event) { Q_UNUSED(event) + if (!m_handler) + return; + // TODO: support attentionIcon/overlayIcon QPalette palette; auto textColor = DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType ? Qt::black : Qt::white;