From 59742f6cd85a5c74970c3ca99bfdbc541091580c Mon Sep 17 00:00:00 2001 From: zhanghongyuan Date: Wed, 30 Sep 2026 16:07:07 +0800 Subject: [PATCH] fix: add null guards and check svg file open in DDropdownMenu - setCurrentAction(): guard against a null m_menu and skip top-level actions that have no submenu before calling menu()->actions(); a submenu-less action (e.g. the 'None' entry) crashed there. - setCurrentTextOnly(): guard m_menu, which is null after deleteMenu()/setMenu(nullptr). - setSvgColor(): check QFile::open() and return an empty pixmap instead of silently rendering a blank arrow icon; drop the unused before_color variable in SetSVGBackColor. Log: Influence: ddropdownmenu widget --- src/widgets/ddropdownmenu.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/widgets/ddropdownmenu.cpp b/src/widgets/ddropdownmenu.cpp index adbcfe77..818570bb 100644 --- a/src/widgets/ddropdownmenu.cpp +++ b/src/widgets/ddropdownmenu.cpp @@ -111,10 +111,19 @@ void DDropdownMenu::setCurrentAction(QAction *pAct) qDebug() << "DDropdownMenu setCurrentAction"; if(pAct){ qDebug() << "pAct is not null"; + if (m_menu == nullptr) { + qDebug() << "DDropdownMenu setCurrentAction: menu is null"; + setText(pAct->text()); + return; + } QList menuList = m_menu->actions(); pAct->setChecked(true); for (int i = 0; i < menuList.size(); i++) { - QList acts = menuList[i]->menu()->actions(); + QMenu *subMenu = menuList[i]->menu(); + if (subMenu == nullptr) { + continue; + } + QList acts = subMenu->actions(); for (int j = 0; j < acts.size(); j++) { if(acts[j] != pAct) acts[j]->setChecked(false); } @@ -145,8 +154,10 @@ void DDropdownMenu::setCurrentTextOnly(const QString &name) // } // } // } - for(auto ac:m_menu->actions()){ - setCheckedExclusive(ac,name); + if (m_menu != nullptr) { + for(auto ac:m_menu->actions()){ + setCheckedExclusive(ac,name); + } } setText(name); @@ -579,7 +590,10 @@ QPixmap DDropdownMenu::setSvgColor(QString color) //设置图标颜色 QString path = QString(":/images/arrow_dark.svg"); QFile file(path); - file.open(QIODevice::ReadOnly); + if (!file.open(QIODevice::ReadOnly)) { + qWarning() << "DDropdownMenu setSvgColor: failed to open" << path << file.errorString(); + return QPixmap(); + } QByteArray data = file.readAll(); QDomDocument doc; doc.setContent(data); @@ -609,7 +623,6 @@ void DDropdownMenu::SetSVGBackColor(QDomElement &elem, QString strattr, QString if (elem.tagName().compare("g") == 0 && elem.attribute("id").compare("color") == 0) { qDebug() << "DDropdownMenu SetSVGBackColor, found color group"; - QString before_color = elem.attribute(strattr); elem.setAttribute(strattr, strattrval); } for (int i = 0; i < elem.childNodes().count(); i++)