File indexing completed on 2025-01-18 10:09:36
0001
0002
0003
0004 #ifndef QMENU_H
0005 #define QMENU_H
0006
0007 #include <QtWidgets/qtwidgetsglobal.h>
0008 #include <QtWidgets/qwidget.h>
0009 #include <QtCore/qstring.h>
0010 #include <QtGui/qicon.h>
0011 #include <QtGui/qaction.h>
0012
0013 #if defined(Q_OS_MACOS) || defined(Q_QDOC)
0014 Q_FORWARD_DECLARE_OBJC_CLASS(NSMenu);
0015 #endif
0016
0017 QT_REQUIRE_CONFIG(menu);
0018
0019 QT_BEGIN_NAMESPACE
0020
0021 class QMenuPrivate;
0022 class QStyleOptionMenuItem;
0023 class QPlatformMenu;
0024
0025 class Q_WIDGETS_EXPORT QMenu : public QWidget
0026 {
0027 private:
0028 Q_OBJECT
0029 Q_DECLARE_PRIVATE(QMenu)
0030
0031 Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
0032 Q_PROPERTY(QString title READ title WRITE setTitle)
0033 Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
0034 Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
0035 Q_PROPERTY(bool toolTipsVisible READ toolTipsVisible WRITE setToolTipsVisible)
0036
0037 public:
0038 explicit QMenu(QWidget *parent = nullptr);
0039 explicit QMenu(const QString &title, QWidget *parent = nullptr);
0040 ~QMenu();
0041
0042 using QWidget::addAction;
0043 #if QT_WIDGETS_REMOVED_SINCE(6, 3)
0044 QAction *addAction(const QString &text);
0045 QAction *addAction(const QIcon &icon, const QString &text);
0046 #if !QT_CONFIG(shortcut)
0047 QAction *addAction(const QString &text, const QObject *receiver, const char* member);
0048 QAction *addAction(const QIcon &icon, const QString &text,
0049 const QObject *receiver, const char* member);
0050 #endif
0051 #endif
0052
0053 #if QT_CONFIG(shortcut)
0054 #if QT_DEPRECATED_SINCE(6, 4)
0055 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, receiver, member) instead.")
0056 QAction *addAction(const QString &text, const QObject *receiver, const char* member,
0057 const QKeySequence &shortcut);
0058 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, receiver, member) instead.")
0059 QAction *addAction(const QIcon &icon, const QString &text,
0060 const QObject *receiver, const char* member,
0061 const QKeySequence &shortcut);
0062
0063 #ifdef Q_QDOC
0064 template<typename Functor>
0065 QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut);
0066 template<typename Functor>
0067 QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut);
0068 template<typename Functor>
0069 QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut);
0070 template<typename Functor>
0071 QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut);
0072 #else
0073
0074 template<class Obj, typename Func1>
0075 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, object, slot) instead.")
0076 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
0077 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
0078 addAction(const QString &text, const Obj *object, Func1 slot,
0079 const QKeySequence &shortcut)
0080 {
0081 return addAction(text, shortcut, object, slot);
0082 }
0083
0084 template <typename Func1>
0085 QT_DEPRECATED_VERSION_X_6_4("Use addAction(text, shortcut, slot) instead.")
0086 inline QAction *addAction(const QString &text, Func1 slot, const QKeySequence &shortcut)
0087 {
0088 return addAction(text, shortcut, slot);
0089 }
0090
0091 template<class Obj, typename Func1>
0092 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, object, slot) instead.")
0093 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
0094 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
0095 addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot,
0096 const QKeySequence &shortcut)
0097
0098 {
0099 return addAction(actionIcon, text, shortcut, object, slot);
0100 }
0101
0102 template <typename Func1>
0103 QT_DEPRECATED_VERSION_X_6_4("Use addAction(icon, text, shortcut, slot) instead.")
0104 inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot,
0105 const QKeySequence &shortcut)
0106 {
0107 return addAction(actionIcon, text, shortcut, slot);
0108 }
0109 #endif
0110 #endif
0111 #endif
0112
0113 QAction *addMenu(QMenu *menu);
0114 QMenu *addMenu(const QString &title);
0115 QMenu *addMenu(const QIcon &icon, const QString &title);
0116
0117 QAction *addSeparator();
0118
0119 QAction *addSection(const QString &text);
0120 QAction *addSection(const QIcon &icon, const QString &text);
0121
0122 QAction *insertMenu(QAction *before, QMenu *menu);
0123 QAction *insertSeparator(QAction *before);
0124 QAction *insertSection(QAction *before, const QString &text);
0125 QAction *insertSection(QAction *before, const QIcon &icon, const QString &text);
0126
0127 bool isEmpty() const;
0128 void clear();
0129
0130 void setTearOffEnabled(bool);
0131 bool isTearOffEnabled() const;
0132
0133 bool isTearOffMenuVisible() const;
0134 void showTearOffMenu();
0135 void showTearOffMenu(const QPoint &pos);
0136 void hideTearOffMenu();
0137
0138 void setDefaultAction(QAction *);
0139 QAction *defaultAction() const;
0140
0141 void setActiveAction(QAction *act);
0142 QAction *activeAction() const;
0143
0144 void popup(const QPoint &pos, QAction *at = nullptr);
0145 QAction *exec();
0146 QAction *exec(const QPoint &pos, QAction *at = nullptr);
0147
0148 static QAction *exec(const QList<QAction *> &actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr);
0149
0150 QSize sizeHint() const override;
0151
0152 QRect actionGeometry(QAction *) const;
0153 QAction *actionAt(const QPoint &) const;
0154
0155 QAction *menuAction() const;
0156 static QMenu *menuInAction(const QAction *action)
0157 { return qobject_cast<QMenu *>(action->menuObject()); }
0158
0159 QString title() const;
0160 void setTitle(const QString &title);
0161
0162 QIcon icon() const;
0163 void setIcon(const QIcon &icon);
0164
0165 void setNoReplayFor(QWidget *widget);
0166 QPlatformMenu *platformMenu();
0167 void setPlatformMenu(QPlatformMenu *platformMenu);
0168
0169 #if defined(Q_OS_MACOS) || defined(Q_QDOC)
0170 NSMenu* toNSMenu();
0171 void setAsDockMenu();
0172 #endif
0173
0174 bool separatorsCollapsible() const;
0175 void setSeparatorsCollapsible(bool collapse);
0176
0177 bool toolTipsVisible() const;
0178 void setToolTipsVisible(bool visible);
0179
0180 Q_SIGNALS:
0181 void aboutToShow();
0182 void aboutToHide();
0183 void triggered(QAction *action);
0184 void hovered(QAction *action);
0185
0186 protected:
0187 int columnCount() const;
0188
0189 void changeEvent(QEvent *) override;
0190 void keyPressEvent(QKeyEvent *) override;
0191 void mouseReleaseEvent(QMouseEvent *) override;
0192 void mousePressEvent(QMouseEvent *) override;
0193 void mouseMoveEvent(QMouseEvent *) override;
0194 #if QT_CONFIG(wheelevent)
0195 void wheelEvent(QWheelEvent *) override;
0196 #endif
0197 void enterEvent(QEnterEvent *) override;
0198 void leaveEvent(QEvent *) override;
0199 void hideEvent(QHideEvent *) override;
0200 void paintEvent(QPaintEvent *) override;
0201 void actionEvent(QActionEvent *) override;
0202 void timerEvent(QTimerEvent *) override;
0203 bool event(QEvent *) override;
0204 bool focusNextPrevChild(bool next) override;
0205 virtual void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
0206
0207 private Q_SLOTS:
0208 void internalDelayedPopup();
0209
0210 private:
0211 Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
0212 Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
0213 Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
0214 Q_PRIVATE_SLOT(d_func(), void _q_platformMenuAboutToShow())
0215
0216 protected:
0217 QMenu(QMenuPrivate &dd, QWidget* parent = nullptr);
0218
0219 private:
0220 Q_DISABLE_COPY(QMenu)
0221
0222 friend class QMenuBar;
0223 friend class QMenuBarPrivate;
0224 friend class QTornOffMenu;
0225 friend class QComboBox;
0226 friend class QtWidgetsActionPrivate;
0227 friend class QToolButtonPrivate;
0228 friend void qt_mac_emit_menuSignals(QMenu *menu, bool show);
0229 friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action);
0230 };
0231
0232 QT_END_NAMESPACE
0233
0234 #endif