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
4#ifndef QMENU_H
5#define QMENU_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtWidgets/qwidget.h>
9#include <QtCore/qstring.h>
10#include <QtGui/qicon.h>
11#include <QtGui/qaction.h>
12
13#if defined(Q_OS_MACOS) || defined(Q_QDOC)
15#endif
16
18
19QT_BEGIN_NAMESPACE
20
21class QMenuPrivate;
22class QStyleOptionMenuItem;
23class QPlatformMenu;
24
25class Q_WIDGETS_EXPORT QMenu : public QWidget
26{
27private:
28 Q_OBJECT
29 Q_DECLARE_PRIVATE(QMenu)
30
31 Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
32 Q_PROPERTY(QString title READ title WRITE setTitle)
33 Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
34 Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
35 Q_PROPERTY(bool toolTipsVisible READ toolTipsVisible WRITE setToolTipsVisible)
36
37public:
38 explicit QMenu(QWidget *parent = nullptr);
39 explicit QMenu(const QString &title, QWidget *parent = nullptr);
40 ~QMenu();
41
42 using QWidget::addAction;
43#if QT_WIDGETS_REMOVED_SINCE(6, 3)
44 QAction *addAction(const QString &text);
45 QAction *addAction(const QIcon &icon, const QString &text);
46#if !QT_CONFIG(shortcut)
47 QAction *addAction(const QString &text, const QObject *receiver, const char* member);
48 QAction *addAction(const QIcon &icon, const QString &text,
49 const QObject *receiver, const char* member);
50#endif
51#endif
52
53#if QT_CONFIG(shortcut)
54#if QT_DEPRECATED_SINCE(6, 4)
55 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, receiver, member) instead.")
56 QAction *addAction(const QString &text, const QObject *receiver, const char* member,
57 const QKeySequence &shortcut);
58 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, receiver, member) instead.")
59 QAction *addAction(const QIcon &icon, const QString &text,
60 const QObject *receiver, const char* member,
61 const QKeySequence &shortcut);
62
63#ifdef Q_QDOC
64 template<typename Functor>
65 QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut);
66 template<typename Functor>
67 QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut);
68 template<typename Functor>
69 QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut);
70 template<typename Functor>
71 QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut);
72#else
73 // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
74 template<class Obj, typename Func1>
75 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, object, slot) instead.")
76 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
77 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
78 addAction(const QString &text, const Obj *object, Func1 slot,
79 const QKeySequence &shortcut)
80 {
81 return addAction(text, shortcut, object, slot);
82 }
83 // addAction(QString): Connect to a functor or function pointer (without context)
84 template <typename Func1>
85 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, slot) instead.")
86 inline QAction *addAction(const QString &text, Func1 slot, const QKeySequence &shortcut)
87 {
88 return addAction(text, shortcut, slot);
89 }
90 // addAction(QIcon, QString): Connect to a QObject slot / functor or function pointer (with context)
91 template<class Obj, typename Func1>
92 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, object, slot) instead.")
93 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
94 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
95 addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot,
96 const QKeySequence &shortcut)
97
98 {
99 return addAction(actionIcon, text, shortcut, object, slot);
100 }
101 // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
102 template <typename Func1>
103 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, slot) instead.")
104 inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot,
105 const QKeySequence &shortcut)
106 {
107 return addAction(actionIcon, text, shortcut, slot);
108 }
109#endif // !Q_QDOC
110#endif // QT_DEPRECATED_SINCE(6, 4)
111#endif // QT_CONFIG(shortcut)
112
113 QAction *addMenu(QMenu *menu);
114 QMenu *addMenu(const QString &title);
115 QMenu *addMenu(const QIcon &icon, const QString &title);
116
117 QAction *addSeparator();
118
119 QAction *addSection(const QString &text);
120 QAction *addSection(const QIcon &icon, const QString &text);
121
122 QAction *insertMenu(QAction *before, QMenu *menu);
123 QAction *insertSeparator(QAction *before);
124 QAction *insertSection(QAction *before, const QString &text);
125 QAction *insertSection(QAction *before, const QIcon &icon, const QString &text);
126
127 bool isEmpty() const;
128 void clear();
129
130 void setTearOffEnabled(bool);
131 bool isTearOffEnabled() const;
132
133 bool isTearOffMenuVisible() const;
134 void showTearOffMenu();
135 void showTearOffMenu(const QPoint &pos);
136 void hideTearOffMenu();
137
138 void setDefaultAction(QAction *);
139 QAction *defaultAction() const;
140
141 void setActiveAction(QAction *act);
142 QAction *activeAction() const;
143
144 void popup(const QPoint &pos, QAction *at = nullptr);
145 QAction *exec();
146 QAction *exec(const QPoint &pos, QAction *at = nullptr);
147
148 static QAction *exec(const QList<QAction *> &actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr);
149
150 QSize sizeHint() const override;
151
152 QRect actionGeometry(QAction *) const;
153 QAction *actionAt(const QPoint &) const;
154
155 QAction *menuAction() const;
156 static QMenu *menuInAction(const QAction *action)
157 { return qobject_cast<QMenu *>(action->menuObject()); }
158
159 QString title() const;
160 void setTitle(const QString &title);
161
162 QIcon icon() const;
163 void setIcon(const QIcon &icon);
164
165 void setNoReplayFor(QWidget *widget);
166 QPlatformMenu *platformMenu();
167 void setPlatformMenu(QPlatformMenu *platformMenu);
168
169#if defined(Q_OS_MACOS) || defined(Q_QDOC)
170 NSMenu* toNSMenu();
171 void setAsDockMenu();
172#endif
173
174 bool separatorsCollapsible() const;
175 void setSeparatorsCollapsible(bool collapse);
176
177 bool toolTipsVisible() const;
178 void setToolTipsVisible(bool visible);
179
180Q_SIGNALS:
181 void aboutToShow();
182 void aboutToHide();
183 void triggered(QAction *action);
184 void hovered(QAction *action);
185
186protected:
187 int columnCount() const;
188
189 void changeEvent(QEvent *) override;
190 void keyPressEvent(QKeyEvent *) override;
191 void mouseReleaseEvent(QMouseEvent *) override;
192 void mousePressEvent(QMouseEvent *) override;
193 void mouseMoveEvent(QMouseEvent *) override;
194#if QT_CONFIG(wheelevent)
195 void wheelEvent(QWheelEvent *) override;
196#endif
197 void enterEvent(QEnterEvent *) override;
198 void leaveEvent(QEvent *) override;
199 void hideEvent(QHideEvent *) override;
200 void paintEvent(QPaintEvent *) override;
201 void actionEvent(QActionEvent *) override;
202 void timerEvent(QTimerEvent *) override;
203 bool event(QEvent *) override;
204 bool focusNextPrevChild(bool next) override;
205 virtual void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
206
207private Q_SLOTS:
208 void internalDelayedPopup();
209
210private:
211 Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
212 Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
213 Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
214 Q_PRIVATE_SLOT(d_func(), void _q_platformMenuAboutToShow())
215
216protected:
217 QMenu(QMenuPrivate &dd, QWidget* parent = nullptr);
218
219private:
220 Q_DISABLE_COPY(QMenu)
221
222 friend class QMenuBar;
223 friend class QMenuBarPrivate;
224 friend class QTornOffMenu;
225 friend class QComboBox;
226 friend class QtWidgetsActionPrivate;
227 friend class QToolButtonPrivate;
228 friend void qt_mac_emit_menuSignals(QMenu *menu, bool show);
229 friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action);
230};
231
232QT_END_NAMESPACE
233
234#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:26
\inmodule QtCore
constexpr QModelIndex() noexcept
Creates a new empty model index.
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
static QString formatNumber(int number, int fieldWidth)
Q_DECLARE_TYPEINFO(QtPrivate::SectionToken, Q_PRIMITIVE_TYPE)
QT_REQUIRE_CONFIG(calendarwidget)
#define qApp
QT_REQUIRE_CONFIG(itemmodel)
constexpr SectionToken(QCalendarDateSectionValidator *v, int rep)
QCalendarDateSectionValidator * validator