1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qaction.h"
5
6#include <private/qapplication_p.h>
7#include <private/qwidget_p.h>
8#include "qaction_widgets_p.h"
9#if QT_CONFIG(menu)
10#include <private/qmenu_p.h>
11#endif
12#if QT_CONFIG(graphicsview)
13#include "qgraphicswidget.h"
14#endif
15
16
17QT_BEGIN_NAMESPACE
18
19QActionPrivate *QApplicationPrivate::createActionPrivate() const
20{
21 return new QtWidgetsActionPrivate;
22}
23
24QtWidgetsActionPrivate::~QtWidgetsActionPrivate() = default;
25
26// we can't do this in the destructor, as it would only be called by ~QObject
27void QtWidgetsActionPrivate::destroy()
28{
29 Q_Q(QAction);
30 const auto objects = associatedObjects;
31 const auto end = objects.crend();
32 for (auto it = objects.crbegin(); it != end; ++it) {
33 QObject *object = *it;
34 if (QWidget *widget = qobject_cast<QWidget*>(o: object))
35 widget->removeAction(action: q);
36#if QT_CONFIG(graphicsview)
37 else if (QGraphicsWidget *graphicsWidget = qobject_cast<QGraphicsWidget*>(object))
38 graphicsWidget->removeAction(action: q);
39#endif
40 }
41}
42
43#if QT_CONFIG(shortcut)
44QShortcutMap::ContextMatcher QtWidgetsActionPrivate::contextMatcher() const
45{
46 return qWidgetShortcutContextMatcher;
47}
48#endif
49
50#if QT_CONFIG(menu)
51QObject *QtWidgetsActionPrivate::menu() const
52{
53 return m_menu;
54}
55
56void QtWidgetsActionPrivate::setMenu(QObject *menu)
57{
58 Q_Q(QAction);
59 QMenu *theMenu = qobject_cast<QMenu*>(object: menu);
60 Q_ASSERT_X(!menu || theMenu, "QAction::setMenu",
61 "QAction::setMenu expects a QMenu* in widget applications");
62 if (m_menu)
63 m_menu->d_func()->setOverrideMenuAction(nullptr); //we reset the default action of any previous menu
64 m_menu = theMenu;
65 if (m_menu)
66 m_menu->d_func()->setOverrideMenuAction(q);
67 sendDataChanged();
68}
69#endif // QT_CONFIG(menu)
70
71QT_END_NAMESPACE
72

source code of qtbase/src/widgets/kernel/qaction_widgets.cpp