From 144bba95e9b29d5edd909049452da47565305e9f Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Wed, 7 Mar 2018 13:47:45 +0100 Subject: [PATCH] ITaskHandler: Harden against vanishing ITaskHandlers Harden against vanishing ITaskHandlers and avoid qobject_casts. Task-number: QTCREATORBUG-19994 Change-Id: I72bccb944cb6fa6ec92a8c0293617931a2eb2732 Reviewed-by: Christian Stenger Reviewed-by: hjk --- src/plugins/projectexplorer/taskwindow.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/plugins/projectexplorer/taskwindow.cpp b/src/plugins/projectexplorer/taskwindow.cpp index 75ba8e63916..6b23ac4e5e7 100644 --- a/src/plugins/projectexplorer/taskwindow.cpp +++ b/src/plugins/projectexplorer/taskwindow.cpp @@ -210,11 +210,18 @@ void TaskView::resizeEvent(QResizeEvent *e) class TaskWindowPrivate { public: + ITaskHandler *handler(const QAction *action) + { + ITaskHandler *handler = m_actionToHandlerMap.value(action, nullptr); + return g_taskHandlers.contains(handler) ? handler : nullptr; + } + Internal::TaskModel *m_model; Internal::TaskFilterModel *m_filter; Internal::TaskView *m_listview; Internal::TaskWindowContext *m_taskWindowContext; QMenu *m_contextMenu; + QMap m_actionToHandlerMap; ITaskHandler *m_defaultHandler = nullptr; QToolButton *m_filterWarningsButton; QToolButton *m_categoriesButton; @@ -318,14 +325,6 @@ TaskWindow::~TaskWindow() delete d; } -static ITaskHandler *handler(QAction *action) -{ - QVariant prop = action->property("ITaskHandler"); - ITaskHandler *handler = qobject_cast(prop.value()); - QTC_CHECK(handler); - return handler; -} - void TaskWindow::delayedInitialization() { static bool alreadyDone = false; @@ -340,7 +339,7 @@ void TaskWindow::delayedInitialization() QAction *action = h->createAction(this); QTC_ASSERT(action, continue); - action->setProperty("ITaskHandler", qVariantFromValue(qobject_cast(h))); + d->m_actionToHandlerMap.insert(action, h); connect(action, &QAction::triggered, this, &TaskWindow::actionTriggered); d->m_actions << action; @@ -395,7 +394,7 @@ void TaskWindow::currentChanged(const QModelIndex &index) { const Task task = index.isValid() ? d->m_filter->task(index) : Task(); foreach (QAction *action, d->m_actions) { - ITaskHandler *h = handler(action); + ITaskHandler *h = d->handler(action); action->setEnabled((task.isNull() || !h) ? false : h->canHandle(task)); } } @@ -511,7 +510,7 @@ void TaskWindow::actionTriggered() auto action = qobject_cast(sender()); if (!action || !action->isEnabled()) return; - ITaskHandler *h = handler(action); + ITaskHandler *h = d->handler(action); if (!h) return;