Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmenu.h
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
5#ifndef QMENU_H
6#define QMENU_H
7
8#include <QtWidgets/qtwidgetsglobal.h>
9#include <QtWidgets/qwidget.h>
10#include <QtCore/qstring.h>
11#include <QtGui/qicon.h>
12#include <QtGui/qaction.h>
13
14#if defined(Q_OS_MACOS) || defined(Q_QDOC)
16#endif
17
19
20QT_BEGIN_NAMESPACE
21
22class QMenuPrivate;
23class QStyleOptionMenuItem;
24class QPlatformMenu;
25
26class Q_WIDGETS_EXPORT QMenu : public QWidget
27{
28private:
29 Q_OBJECT
30 Q_DECLARE_PRIVATE(QMenu)
31
32 Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
33 Q_PROPERTY(QString title READ title WRITE setTitle)
34 Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
35 Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
36 Q_PROPERTY(bool toolTipsVisible READ toolTipsVisible WRITE setToolTipsVisible)
37
38public:
39 explicit QMenu(QWidget *parent = nullptr);
40 explicit QMenu(const QString &title, QWidget *parent = nullptr);
41 ~QMenu();
42
43 using QWidget::addAction;
44#if QT_WIDGETS_REMOVED_SINCE(6, 3)
45 QAction *addAction(const QString &text);
46 QAction *addAction(const QIcon &icon, const QString &text);
47#if !QT_CONFIG(shortcut)
48 QAction *addAction(const QString &text, const QObject *receiver, const char* member);
49 QAction *addAction(const QIcon &icon, const QString &text,
50 const QObject *receiver, const char* member);
51#endif
52#endif
53
54#if QT_CONFIG(shortcut)
55#if QT_DEPRECATED_SINCE(6, 4)
56 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, receiver, member) instead.")
57 QAction *addAction(const QString &text, const QObject *receiver, const char* member,
58 const QKeySequence &shortcut);
59 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, receiver, member) instead.")
60 QAction *addAction(const QIcon &icon, const QString &text,
61 const QObject *receiver, const char* member,
62 const QKeySequence &shortcut);
63
64#ifdef Q_QDOC
65 template<typename Functor>
66 QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut);
67 template<typename Functor>
68 QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut);
69 template<typename Functor>
70 QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut);
71 template<typename Functor>
72 QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut);
73#else
74 // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
75 template<class Obj, typename Func1>
76 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, object, slot) instead.")
77 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
78 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
79 addAction(const QString &text, const Obj *object, Func1 slot,
80 const QKeySequence &shortcut)
81 {
82 return addAction(text, shortcut, object, slot);
83 }
84 // addAction(QString): Connect to a functor or function pointer (without context)
85 template <typename Func1>
86 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, slot) instead.")
87 inline QAction *addAction(const QString &text, Func1 slot, const QKeySequence &shortcut)
88 {
89 return addAction(text, shortcut, slot);
90 }
91 // addAction(QIcon, QString): Connect to a QObject slot / functor or function pointer (with context)
92 template<class Obj, typename Func1>
93 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, object, slot) instead.")
94 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
95 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
96 addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot,
97 const QKeySequence &shortcut)
98
99 {
100 return addAction(actionIcon, text, shortcut, object, slot);
101 }
102 // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
103 template <typename Func1>
104 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, slot) instead.")
105 inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot,
106 const QKeySequence &shortcut)
107 {
108 return addAction(actionIcon, text, shortcut, slot);
109 }
110#endif // !Q_QDOC
111#endif // QT_DEPRECATED_SINCE(6, 4)
112#endif // QT_CONFIG(shortcut)
113
114 QAction *addMenu(QMenu *menu);
115 QMenu *addMenu(const QString &title);
116 QMenu *addMenu(const QIcon &icon, const QString &title);
117
118 QAction *addSeparator();
119
120 QAction *addSection(const QString &text);
121 QAction *addSection(const QIcon &icon, const QString &text);
122
123 QAction *insertMenu(QAction *before, QMenu *menu);
124 QAction *insertSeparator(QAction *before);
125 QAction *insertSection(QAction *before, const QString &text);
126 QAction *insertSection(QAction *before, const QIcon &icon, const QString &text);
127
128 bool isEmpty() const;
129 void clear();
130
131 void setTearOffEnabled(bool);
132 bool isTearOffEnabled() const;
133
134 bool isTearOffMenuVisible() const;
135 void showTearOffMenu();
136 void showTearOffMenu(const QPoint &pos);
137 void hideTearOffMenu();
138
139 void setDefaultAction(QAction *);
140 QAction *defaultAction() const;
141
142 void setActiveAction(QAction *act);
143 QAction *activeAction() const;
144
145 void popup(const QPoint &pos, QAction *at = nullptr);
146 QAction *exec();
147 QAction *exec(const QPoint &pos, QAction *at = nullptr);
148
149 static QAction *exec(const QList<QAction *> &actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr);
150
151 QSize sizeHint() const override;
152
153 QRect actionGeometry(QAction *) const;
154 QAction *actionAt(const QPoint &) const;
155
156 QAction *menuAction() const;
157 static QMenu *menuInAction(const QAction *action)
158 { return qobject_cast<QMenu *>(action->menuObject()); }
159
160 QString title() const;
161 void setTitle(const QString &title);
162
163 QIcon icon() const;
164 void setIcon(const QIcon &icon);
165
166 void setNoReplayFor(QWidget *widget);
167 QPlatformMenu *platformMenu();
168 void setPlatformMenu(QPlatformMenu *platformMenu);
169
170#if defined(Q_OS_MACOS) || defined(Q_QDOC)
171 NSMenu* toNSMenu();
172 void setAsDockMenu();
173#endif
174
175 bool separatorsCollapsible() const;
176 void setSeparatorsCollapsible(bool collapse);
177
178 bool toolTipsVisible() const;
179 void setToolTipsVisible(bool visible);
180
181Q_SIGNALS:
182 void aboutToShow();
183 void aboutToHide();
184 void triggered(QAction *action);
185 void hovered(QAction *action);
186
187protected:
188 int columnCount() const;
189
190 void changeEvent(QEvent *) override;
191 void keyPressEvent(QKeyEvent *) override;
192 void mouseReleaseEvent(QMouseEvent *) override;
193 void mousePressEvent(QMouseEvent *) override;
194 void mouseMoveEvent(QMouseEvent *) override;
195#if QT_CONFIG(wheelevent)
196 void wheelEvent(QWheelEvent *) override;
197#endif
198 void enterEvent(QEnterEvent *) override;
199 void leaveEvent(QEvent *) override;
200 void hideEvent(QHideEvent *) override;
201 void paintEvent(QPaintEvent *) override;
202 void actionEvent(QActionEvent *) override;
203 void timerEvent(QTimerEvent *) override;
204 bool event(QEvent *) override;
205 bool focusNextPrevChild(bool next) override;
206 virtual void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
207
208private Q_SLOTS:
209 void internalDelayedPopup();
210
211private:
212 Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
213 Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
214 Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
215 Q_PRIVATE_SLOT(d_func(), void _q_platformMenuAboutToShow())
216
217protected:
218 QMenu(QMenuPrivate &dd, QWidget* parent = nullptr);
219
220private:
221 Q_DISABLE_COPY(QMenu)
222
223 friend class QMenuBar;
224 friend class QMenuBarPrivate;
225 friend class QTornOffMenu;
226 friend class QComboBox;
227 friend class QtWidgetsActionPrivate;
228 friend class QToolButtonPrivate;
229 friend void qt_mac_emit_menuSignals(QMenu *menu, bool show);
230 friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action);
231};
232
233QT_END_NAMESPACE
234
235#endif // QMENU_H
QCalendarTextNavigator * m_navigator
void setNavigatorEnabled(bool enable)
void paintCell(QPainter *painter, const QRect &rect, QDate date) const
QItemSelectionModel * m_selection
QMap< int, QAction * > monthToAction
void showMonth(int year, int month)
void _q_monthChanged(QAction *)
void _q_slotChangeDate(QDate date, bool changeMonth)
void createNavigationBar(QWidget *widget)
void _q_slotShowDate(QDate date)
QCalendarDelegate * m_delegate
void updateCurrentPage(QDate newDate)
void _q_slotChangeDate(QDate date)
The QCalendarWidget class provides a monthly based calendar widget allowing the user to select a date...
The QCalendar class describes calendar systems.
Definition qcalendar.h:53
Definition qmap.h:189
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:27
\inmodule QtCore
constexpr QModelIndex() noexcept
Creates a new empty model index.
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget.
void paintEvent(QPaintEvent *e) override
\reimp
virtual Section handleKey(int key)=0
virtual void setDate(QDate date, QCalendar cal=QCalendar())=0
virtual QDate applyToDate(QDate date, QCalendar cal=QCalendar()) const =0
virtual QString text(QDate date, QCalendar cal, int repeat) const =0
static QString highlightString(const QString &str, int pos)
virtual QString text() const =0
void setLocale(const QLocale &locale)
void setInitialDate(QDate date, QCalendar cal)
void handleKeyEvent(QKeyEvent *keyEvent, QCalendar cal)
void setFormat(const QString &format)
QString currentText(QCalendar cal) const
virtual QDate applyToDate(QDate date, QCalendar cal) const override
virtual Section handleKey(int key) override
virtual void setDate(QDate date, QCalendar cal) override
virtual QString text() const override
virtual QString text(QDate date, QCalendar cal, int repeat) const override
void paintCell(QPainter *painter, const QRect &rect, QDate date) const
QString dayName(Qt::DayOfWeek day) const
void showMonth(int year, int month)
QTextCharFormat formatForCell(int row, int col) const
int columnCount(const QModelIndex &parent) const override
Returns the number of columns for the children of the given parent.
QString monthName(const QLocale &locale, int month)
void setFirstColumnDay(Qt::DayOfWeek dayOfWeek)
int columnForDayOfWeek(Qt::DayOfWeek day) const
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
StaticDayOfWeekAssociativeArray< QTextCharFormat > m_dayFormats
int columnForFirstOfMonth(QDate date) const
void cellForDate(QDate date, int *row, int *column) const
QVariant data(const QModelIndex &index, int role) const override
Returns the data stored under the given role for the item referred to by the index.
Qt::DayOfWeek firstColumnDay() const
void setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format)
Qt::DayOfWeek dayOfWeekForColumn(int section) const
void setView(QCalendarView *view)
int rowCount(const QModelIndex &parent) const override
Returns the number of rows under the given parent.
void setRange(QDate min, QDate max)
QMap< QDate, QTextCharFormat > m_dateFormats
QDate dateForCell(int row, int column) const
virtual QDate applyToDate(QDate date, QCalendar cal) const override
virtual Section handleKey(int key) override
virtual QString text(QDate date, QCalendar cal, int repeat) const override
virtual void setDate(QDate date, QCalendar cal) override
virtual QString text() const override
void timerEvent(QTimerEvent *e) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
bool eventFilter(QObject *o, QEvent *e) override
Filters events if this object has been installed as an event filter for the watched object.
virtual void keyboardSearch(const QString &) override
Moves to and selects the item best matching the string search.
void mouseReleaseEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
bool event(QEvent *event) override
void keyPressEvent(QKeyEvent *event) override
void changeDate(QDate date, bool changeMonth)
void setReadOnly(bool enable)
QDate handleMouseEvent(QMouseEvent *event)
QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override
Returns a QModelIndex object pointing to the next object in the view, based on the given cursorAction...
void clicked(QDate date)
void mouseDoubleClickEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
virtual QDate applyToDate(QDate date, QCalendar cal) const override
virtual QString text(QDate date, QCalendar cal, int repeat) const override
virtual void setDate(QDate date, QCalendar cal) override
virtual Section handleKey(int key) override
virtual QString text() const override
@ RowCount
@ MinimumDayOffset
@ HeaderRow
@ HeaderColumn
@ ColumnCount
QtPrivate::QPrevNextCalButton QPrevNextCalButton
QtPrivate::QCalendarDateSectionValidator QCalendarDateSectionValidator
QtPrivate::QCalendarModel QCalendarModel
QtPrivate::QCalendarDayValidator QCalendarDayValidator
QtPrivate::QCalendarYearValidator QCalendarYearValidator
QtPrivate::QCalendarDelegate QCalendarDelegate
QtPrivate::QCalendarView QCalendarView
static QString formatNumber(int number, int fieldWidth)
QtPrivate::QCalendarTextNavigator QCalendarTextNavigator
Q_DECLARE_TYPEINFO(QtPrivate::SectionToken, Q_PRIMITIVE_TYPE)
QtPrivate::QCalendarMonthValidator QCalendarMonthValidator
QtPrivate::QCalToolButton QCalToolButton
QtPrivate::QCalendarDateValidator QCalendarDateValidator
QT_REQUIRE_CONFIG(calendarwidget)
#define qApp
QT_REQUIRE_CONFIG(thread)
constexpr SectionToken(QCalendarDateSectionValidator *v, int rep)
QCalendarDateSectionValidator * validator