QtBase  v6.3.1
qmenu.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QMENU_H
41 #define QMENU_H
42 
43 #include <QtWidgets/qtwidgetsglobal.h>
44 #include <QtWidgets/qwidget.h>
45 #include <QtCore/qstring.h>
46 #include <QtGui/qicon.h>
47 #include <QtGui/qaction.h>
48 
49 #if defined(Q_OS_MACOS) || defined(Q_CLANG_QDOC)
51 #endif
52 
54 
56 
57 class QMenuPrivate;
59 class QPlatformMenu;
60 
61 class Q_WIDGETS_EXPORT QMenu : public QWidget
62 {
63 private:
64  Q_OBJECT
65  Q_DECLARE_PRIVATE(QMenu)
66 
67  Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
69  Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
70  Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
71  Q_PROPERTY(bool toolTipsVisible READ toolTipsVisible WRITE setToolTipsVisible)
72 
73 public:
74  explicit QMenu(QWidget *parent = nullptr);
75  explicit QMenu(const QString &title, QWidget *parent = nullptr);
76  ~QMenu();
77 
78  using QWidget::addAction;
79 #if QT_WIDGETS_REMOVED_SINCE(6, 3)
80  QAction *addAction(const QString &text);
81  QAction *addAction(const QIcon &icon, const QString &text);
82 #if !QT_CONFIG(shortcut)
83  QAction *addAction(const QString &text, const QObject *receiver, const char* member);
84  QAction *addAction(const QIcon &icon, const QString &text,
85  const QObject *receiver, const char* member);
86 #endif
87 #endif
88 
89 #if QT_CONFIG(shortcut)
90 #if QT_DEPRECATED_SINCE(6, 4)
91  QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, receiver, member) instead.")
92  QAction *addAction(const QString &text, const QObject *receiver, const char* member,
93  const QKeySequence &shortcut);
94  QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, receiver, member) instead.")
95  QAction *addAction(const QIcon &icon, const QString &text,
96  const QObject *receiver, const char* member,
97  const QKeySequence &shortcut);
98 
99 #ifdef Q_CLANG_QDOC
100  template<typename Functor>
101  QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut);
102  template<typename Functor>
103  QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut);
104  template<typename Functor>
105  QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut);
106  template<typename Functor>
107  QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut);
108 #else
109  // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
110  template<class Obj, typename Func1>
111  QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, object, slot) instead.")
114  addAction(const QString &text, const Obj *object, Func1 slot,
115  const QKeySequence &shortcut)
116  {
117  return addAction(text, shortcut, object, slot);
118  }
119  // addAction(QString): Connect to a functor or function pointer (without context)
120  template <typename Func1>
121  QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, slot) instead.")
122  inline QAction *addAction(const QString &text, Func1 slot, const QKeySequence &shortcut)
123  {
124  return addAction(text, shortcut, slot);
125  }
126  // addAction(QIcon, QString): Connect to a QObject slot / functor or function pointer (with context)
127  template<class Obj, typename Func1>
128  QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, object, slot) instead.")
131  addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot,
132  const QKeySequence &shortcut)
133 
134  {
135  return addAction(actionIcon, text, shortcut, object, slot);
136  }
137  // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
138  template <typename Func1>
139  QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, slot) instead.")
140  inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot,
141  const QKeySequence &shortcut)
142  {
143  return addAction(actionIcon, text, shortcut, slot);
144  }
145 #endif // !Q_CLANG_QDOC
146 #endif // QT_DEPRECATED_SINCE(6, 4)
147 #endif // QT_CONFIG(shortcut)
148 
150  QMenu *addMenu(const QString &title);
151  QMenu *addMenu(const QIcon &icon, const QString &title);
152 
153  QAction *addSeparator();
154 
155  QAction *addSection(const QString &text);
156  QAction *addSection(const QIcon &icon, const QString &text);
157 
158  QAction *insertMenu(QAction *before, QMenu *menu);
159  QAction *insertSeparator(QAction *before);
160  QAction *insertSection(QAction *before, const QString &text);
161  QAction *insertSection(QAction *before, const QIcon &icon, const QString &text);
162 
163  bool isEmpty() const;
164  void clear();
165 
166  void setTearOffEnabled(bool);
167  bool isTearOffEnabled() const;
168 
169  bool isTearOffMenuVisible() const;
170  void showTearOffMenu();
171  void showTearOffMenu(const QPoint &pos);
172  void hideTearOffMenu();
173 
174  void setDefaultAction(QAction *);
175  QAction *defaultAction() const;
176 
177  void setActiveAction(QAction *act);
178  QAction *activeAction() const;
179 
180  void popup(const QPoint &pos, QAction *at = nullptr);
181  QAction *exec();
182  QAction *exec(const QPoint &pos, QAction *at = nullptr);
183 
184  static QAction *exec(const QList<QAction *> &actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr);
185 
186  QSize sizeHint() const override;
187 
188  QRect actionGeometry(QAction *) const;
189  QAction *actionAt(const QPoint &) const;
190 
191  QAction *menuAction() const;
193  { return qobject_cast<QMenu *>(action->menuObject()); }
194 
195  QString title() const;
196  void setTitle(const QString &title);
197 
198  QIcon icon() const;
199  void setIcon(const QIcon &icon);
200 
201  void setNoReplayFor(QWidget *widget);
203  void setPlatformMenu(QPlatformMenu *platformMenu);
204 
205 #if defined(Q_OS_MACOS) || defined(Q_CLANG_QDOC)
206  NSMenu* toNSMenu();
207  void setAsDockMenu();
208 #endif
209 
210  bool separatorsCollapsible() const;
211  void setSeparatorsCollapsible(bool collapse);
212 
213  bool toolTipsVisible() const;
214  void setToolTipsVisible(bool visible);
215 
216 Q_SIGNALS:
217  void aboutToShow();
218  void aboutToHide();
221 
222 protected:
223  int columnCount() const;
224 
225  void changeEvent(QEvent *) override;
226  void keyPressEvent(QKeyEvent *) override;
227  void mouseReleaseEvent(QMouseEvent *) override;
228  void mousePressEvent(QMouseEvent *) override;
229  void mouseMoveEvent(QMouseEvent *) override;
230 #if QT_CONFIG(wheelevent)
231  void wheelEvent(QWheelEvent *) override;
232 #endif
233  void enterEvent(QEnterEvent *) override;
234  void leaveEvent(QEvent *) override;
235  void hideEvent(QHideEvent *) override;
236  void paintEvent(QPaintEvent *) override;
237  void actionEvent(QActionEvent *) override;
238  void timerEvent(QTimerEvent *) override;
239  bool event(QEvent *) override;
240  bool focusNextPrevChild(bool next) override;
241  virtual void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
242 
243 private Q_SLOTS:
244  void internalDelayedPopup();
245 
246 private:
247  Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
248  Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
249  Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
250  Q_PRIVATE_SLOT(d_func(), void _q_platformMenuAboutToShow())
251 
252 protected:
253  QMenu(QMenuPrivate &dd, QWidget* parent = nullptr);
254 
255 private:
257 
258  friend class QMenuBar;
259  friend class QMenuBarPrivate;
260  friend class QTornOffMenu;
261  friend class QComboBox;
263  friend class QToolButtonPrivate;
264  friend void qt_mac_emit_menuSignals(QMenu *menu, bool show);
265  friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action);
266 };
267 
269 
270 #endif // QMENU_H
#define value
[5]
The QActionEvent class provides an event that is generated when a QAction is added,...
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition: qaction.h:65
The QComboBox widget is a combined button and popup list.
Definition: qcombobox.h:60
The QEnterEvent class contains parameters that describe an enter event.
Definition: qevent.h:197
The QEvent class is the base class of all event classes. Event objects contain event parameters.
Definition: qcoreevent.h:58
The QHideEvent class provides an event which is sent after a widget is hidden.
Definition: qevent.h:656
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:56
The QKeyEvent class describes a key event.
Definition: qevent.h:471
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:71
The QMenuBar class provides a horizontal menu bar.
Definition: qmenubar.h:56
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition: qmenu.h:62
void hovered(QAction *action)
void triggered(QAction *action)
void aboutToShow()
void aboutToHide()
static QMenu * menuInAction(const QAction *action)
Definition: qmenu.h:192
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:231
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
virtual void timerEvent(QTimerEvent *event)
Definition: qobject.cpp:1399
The QPaintEvent class contains event parameters for paint events. \inmodule QtGui.
Definition: qevent.h:539
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:52
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:59
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
The QString class provides a Unicode character string.
Definition: qstring.h:388
the minimum value for the progress bar
Definition: qstyleoption.h:369
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:367
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
virtual void hideEvent(QHideEvent *event)
Definition: qwidget.cpp:10004
virtual void leaveEvent(QEvent *event)
Definition: qwidget.cpp:9632
virtual void mouseMoveEvent(QMouseEvent *event)
Definition: qwidget.cpp:9362
virtual void actionEvent(QActionEvent *event)
Definition: qwidget.cpp:9736
virtual void mousePressEvent(QMouseEvent *event)
Definition: qwidget.cpp:9384
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: qwidget.cpp:9409
virtual bool focusNextPrevChild(bool next)
Definition: qwidget.cpp:6759
virtual void enterEvent(QEnterEvent *event)
Definition: qwidget.cpp:9616
virtual void changeEvent(QEvent *)
Definition: qwidget.cpp:9283
QSize sizeHint
the recommended size for the widget
Definition: qwidget.h:182
bool event(QEvent *event) override
Definition: qwidget.cpp:8772
virtual void keyPressEvent(QKeyEvent *event)
Definition: qwidget.cpp:9507
virtual void paintEvent(QPaintEvent *event)
Definition: qwidget.cpp:9684
void addAction(QAction *action)
Definition: qwidget.cpp:3129
msgBox exec()
QOpenGLWidget * widget
[1]
b clear()
short next
Definition: keywords.cpp:454
action
Definition: devices.py:78
NSMenu QCocoaMenu * platformMenu
Definition: qcocoansmenu.h:65
Q_FORWARD_DECLARE_OBJC_CLASS(NSObject)
#define QT_DEPRECATED_VERSION_X_6_4(text)
Definition: qglobal.h:476
#define Q_DISABLE_COPY(Class)
Definition: qglobal.h:515
@ text
QT_REQUIRE_CONFIG(menu)
GLenum type
Definition: qopengl.h:270
GLuint GLenum option
Definition: qopenglext.h:5929
#define Q_PROPERTY(...)
Definition: qtmetamacros.h:92
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define Q_SLOTS
Definition: qtmetamacros.h:80
#define Q_PRIVATE_SLOT(d, signature)
Definition: qtmetamacros.h:82
#define Q_SIGNALS
Definition: qtmetamacros.h:81
view show()
[18] //! [19]
QIcon icon
[15]
QString title
[35]
myAction setIcon(SomeIcon)
g setTitle("&User information")
[0]
QMenu menu
[5]
QAction * at
menubar addMenu(fileMenu)
[0]
#define private
Definition: main.cpp:37
const int columnCount
Definition: testtable2.cpp:31
#define protected
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent