Qt 4.8
qmenubar.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QMENUBAR_H
43 #define QMENUBAR_H
44 
45 #include <QtGui/qmenu.h>
46 
48 
50 
51 QT_MODULE(Gui)
52 
53 #ifndef QT_NO_MENUBAR
54 
55 class QMenuBarPrivate;
57 class QWindowsStyle;
58 #ifdef QT3_SUPPORT
59 class QMenuItem;
60 #endif
61 
63 {
64  Q_OBJECT
65 
66  bool defaultUp;
68 
69 public:
70  explicit QMenuBar(QWidget *parent = 0);
71  ~QMenuBar();
72 
73 #ifdef Q_NO_USING_KEYWORD
74  void addAction(QAction *action) { QWidget::addAction(action); }
75 #else
76  using QWidget::addAction;
77 #endif
78  QAction *addAction(const QString &text);
79  QAction *addAction(const QString &text, const QObject *receiver, const char* member);
80 
81  QAction *addMenu(QMenu *menu);
82  QMenu *addMenu(const QString &title);
83  QMenu *addMenu(const QIcon &icon, const QString &title);
84 
85 
86  QAction *addSeparator();
87  QAction *insertSeparator(QAction *before);
88 
89  QAction *insertMenu(QAction *before, QMenu *menu);
90 
91  void clear();
92 
93  QAction *activeAction() const;
94  void setActiveAction(QAction *action);
95 
96  void setDefaultUp(bool);
97  bool isDefaultUp() const;
98 
99  QSize sizeHint() const;
100  QSize minimumSizeHint() const;
101  int heightForWidth(int) const;
102 
103  QRect actionGeometry(QAction *) const;
104  QAction *actionAt(const QPoint &) const;
105 
106  void setCornerWidget(QWidget *w, Qt::Corner corner = Qt::TopRightCorner);
107  QWidget *cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const;
108 
109 #ifdef Q_WS_MAC
110  OSMenuRef macMenu();
111  static bool macUpdateMenuBar();
112 #endif
113 
114 #ifdef Q_WS_WINCE
115  void setDefaultAction(QAction *);
116  QAction *defaultAction() const;
117 
118  static void wceCommands(uint command);
119  static void wceRefresh();
120 #endif
121 
122  bool isNativeMenuBar() const;
123  void setNativeMenuBar(bool nativeMenuBar);
124 
125 public Q_SLOTS:
126  virtual void setVisible(bool visible);
127 
128 Q_SIGNALS:
129  void triggered(QAction *action);
130  void hovered(QAction *action);
131 
132 protected:
133  void changeEvent(QEvent *);
134  void keyPressEvent(QKeyEvent *);
137  void mouseMoveEvent(QMouseEvent *);
138  void leaveEvent(QEvent *);
139  void paintEvent(QPaintEvent *);
140  void resizeEvent(QResizeEvent *);
141  void actionEvent(QActionEvent *);
142  void focusOutEvent(QFocusEvent *);
143  void focusInEvent(QFocusEvent *);
144  void timerEvent(QTimerEvent *);
145  bool eventFilter(QObject *, QEvent *);
146  bool event(QEvent *);
147  void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
148 
149 public:
150 #ifdef QT3_SUPPORT
151  QT3_SUPPORT_CONSTRUCTOR QMenuBar(QWidget *parent, const char *name);
152  inline QT3_SUPPORT uint count() const { return actions().count(); }
153  inline QT3_SUPPORT int insertItem(const QString &text, const QObject *receiver, const char* member,
154  const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
155  return insertAny(0, &text, receiver, member, &shortcut, 0, id, index);
156  }
157  inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text,
158  const QObject *receiver, const char* member,
159  const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
160  return insertAny(&icon, &text, receiver, member, &shortcut, 0, id, index);
161  }
162  inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member,
163  const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
164  QIcon icon(pixmap);
165  return insertAny(&icon, 0, receiver, member, &shortcut, 0, id, index);
166  }
167  inline QT3_SUPPORT int insertItem(const QString &text, int id=-1, int index=-1) {
168  return insertAny(0, &text, 0, 0, 0, 0, id, index);
169  }
170  inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, int id=-1, int index=-1) {
171  return insertAny(&icon, &text, 0, 0, 0, 0, id, index);
172  }
173  inline QT3_SUPPORT int insertItem(const QString &text, QMenu *popup, int id=-1, int index=-1) {
174  return insertAny(0, &text, 0, 0, 0, popup, id, index);
175  }
176  inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id=-1, int index=-1) {
177  return insertAny(&icon, &text, 0, 0, 0, popup, id, index);
178  }
179  inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, int id=-1, int index=-1) {
180  QIcon icon(pixmap);
181  return insertAny(&icon, 0, 0, 0, 0, 0, id, index);
182  }
183  inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, QMenu *popup, int id=-1, int index=-1) {
184  QIcon icon(pixmap);
185  return insertAny(&icon, 0, 0, 0, 0, popup, id, index);
186  }
187  QT3_SUPPORT int insertSeparator(int index=-1);
188  inline QT3_SUPPORT void removeItem(int id) {
189  if(QAction *act = findActionForId(id))
190  removeAction(act); }
191  inline QT3_SUPPORT void removeItemAt(int index) {
192  if(QAction *act = actions().value(index))
193  removeAction(act); }
194 #ifndef QT_NO_SHORTCUT
195  inline QT3_SUPPORT QKeySequence accel(int id) const {
196  if(QAction *act = findActionForId(id))
197  return act->shortcut();
198  return QKeySequence(); }
199  inline QT3_SUPPORT void setAccel(const QKeySequence& key, int id) {
200  if(QAction *act = findActionForId(id))
201  act->setShortcut(key);
202  }
203 #endif
204  inline QT3_SUPPORT QIcon iconSet(int id) const {
205  if(QAction *act = findActionForId(id))
206  return act->icon();
207  return QIcon(); }
208  inline QT3_SUPPORT QString text(int id) const {
209  if(QAction *act = findActionForId(id))
210  return act->text();
211  return QString(); }
212  inline QT3_SUPPORT QPixmap pixmap(int id) const {
213  if(QAction *act = findActionForId(id))
214  return act->icon().pixmap(QSize(22,22));
215  return QPixmap(); }
216  inline QT3_SUPPORT void setWhatsThis(int id, const QString &w) {
217  if(QAction *act = findActionForId(id))
218  act->setWhatsThis(w); }
219  inline QT3_SUPPORT QString whatsThis(int id) const {
220  if(QAction *act = findActionForId(id))
221  return act->whatsThis();
222  return QString(); }
223 
224  inline QT3_SUPPORT void changeItem(int id, const QString &text) {
225  if(QAction *act = findActionForId(id))
226  act->setText(text); }
227  inline QT3_SUPPORT void changeItem(int id, const QPixmap &pixmap) {
228  if(QAction *act = findActionForId(id))
229  act->setIcon(QIcon(pixmap)); }
230  inline QT3_SUPPORT void changeItem(int id, const QIcon &icon, const QString &text) {
231  if(QAction *act = findActionForId(id)) {
232  act->setIcon(icon);
233  act->setText(text);
234  }
235  }
236  inline QT3_SUPPORT bool isItemActive(int id) const { return findActionForId(id) == activeAction(); }
237  inline QT3_SUPPORT bool isItemEnabled(int id) const {
238  if(QAction *act = findActionForId(id))
239  return act->isEnabled();
240  return false; }
241  inline QT3_SUPPORT void setItemEnabled(int id, bool enable) {
242  if(QAction *act = findActionForId(id))
243  act->setEnabled(enable); }
244  inline QT3_SUPPORT bool isItemChecked(int id) const {
245  if(QAction *act = findActionForId(id))
246  return act->isChecked();
247  return false; }
248  inline QT3_SUPPORT void setItemChecked(int id, bool check) {
249  if(QAction *act = findActionForId(id))
250  act->setChecked(check); }
251  inline QT3_SUPPORT bool isItemVisible(int id) const {
252  if(QAction *act = findActionForId(id))
253  return act->isVisible();
254  return false; }
255  inline QT3_SUPPORT void setItemVisible(int id, bool visible) {
256  if(QAction *act = findActionForId(id))
257  act->setVisible(visible); }
258  inline QT3_SUPPORT int indexOf(int id) const { return actions().indexOf(findActionForId(id)); }
259  inline QT3_SUPPORT int idAt(int index) const {
260  return index >= 0 && index < actions().size()
261  ? findIdForAction(actions().at(index))
262  : -1;
263  }
264  inline QT3_SUPPORT void activateItemAt(int index) {
265  if(QAction *ret = actions().value(index))
266  setActiveAction(ret);
267  }
268  inline QT3_SUPPORT bool connectItem(int id, const QObject *receiver, const char* member) {
269  if(QAction *act = findActionForId(id)) {
270  QObject::connect(act, SIGNAL(triggered()), receiver, member);
271  return true;
272  }
273  return false;
274  }
275  inline QT3_SUPPORT bool disconnectItem(int id,const QObject *receiver, const char* member) {
276  if(QAction *act = findActionForId(id)) {
277  QObject::disconnect(act, SIGNAL(triggered()), receiver, member);
278  return true;
279  }
280  return false;
281  }
282  inline QT3_SUPPORT QMenuItem *findItem(int id) const {
283  return (QMenuItem*)findActionForId(id);
284  }
285  QT3_SUPPORT bool setItemParameter(int id, int param);
286  QT3_SUPPORT int itemParameter(int id) const;
287 
288  //frame
289  QT3_SUPPORT int frameWidth() const;
290 
291  QT3_SUPPORT void setFrameRect(QRect) {}
292  QT3_SUPPORT QRect frameRect() const { return QRect(); }
293  enum DummyFrame { Box, Sunken, Plain, Raised, MShadow, NoFrame, Panel, StyledPanel,
294  HLine, VLine, GroupBoxPanel, WinPanel, ToolBarPanel, MenuBarPanel,
295  PopupPanel, LineEditPanel, TabWidgetPanel, MShape };
296  QT3_SUPPORT void setFrameShadow(DummyFrame) {}
297  QT3_SUPPORT DummyFrame frameShadow() const { return Plain; }
298  QT3_SUPPORT void setFrameShape(DummyFrame) {}
299  QT3_SUPPORT DummyFrame frameShape() const { return NoFrame; }
300  QT3_SUPPORT void setFrameStyle(int) {}
301  QT3_SUPPORT int frameStyle() const { return 0; }
302  QT3_SUPPORT void setLineWidth(int) {}
303  QT3_SUPPORT int lineWidth() const { return 0; }
304  QT3_SUPPORT void setMargin(int margin) { setContentsMargins(margin, margin, margin, margin); }
305  QT3_SUPPORT int margin() const
306  { int margin; int dummy; getContentsMargins(&margin, &dummy, &dummy, &dummy); return margin; }
307  QT3_SUPPORT void setMidLineWidth(int) {}
308  QT3_SUPPORT int midLineWidth() const { return 0; }
309 
310  //menubar
311  enum Separator { Never=0, InWindowsStyle=1 };
312  inline QT3_SUPPORT Separator separator() const { return InWindowsStyle; }
313  inline QT3_SUPPORT void setSeparator(Separator) { }
314 
315  QT3_SUPPORT void setAutoGeometry(bool);
316  QT3_SUPPORT bool autoGeometry() const;
317 
318 Q_SIGNALS:
319  QT_MOC_COMPAT void activated(int itemId);
320  QT_MOC_COMPAT void highlighted(int itemId);
321 
322 protected:
323  inline QT3_SUPPORT QRect itemRect(int index) {
324  if(QAction *act = actions().value(index))
325  return actionGeometry(act);
326  return QRect();
327  }
328  inline QT3_SUPPORT int itemAtPos(const QPoint &p) {
329  return findIdForAction(actionAt(p));
330  }
331 private:
332  QAction *findActionForId(int id) const;
333  int insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
334  const QKeySequence *shorcut, const QMenu *popup, int id, int index);
335  int findIdForAction(QAction*) const;
336 #endif
337 
338 private:
341  Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
342  Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
343  Q_PRIVATE_SLOT(d_func(), void _q_internalShortcutActivated(int))
344  Q_PRIVATE_SLOT(d_func(), void _q_updateLayout())
345 
346 #ifdef Q_WS_WINCE
347  Q_PRIVATE_SLOT(d_func(), void _q_updateDefaultAction())
348 #endif
349 
350  friend class QMenu;
351  friend class QMenuPrivate;
352  friend class QWindowsStyle;
353 
354 #ifdef Q_WS_MAC
355  friend class QApplicationPrivate;
356  friend class QWidgetPrivate;
358 #endif
359 };
360 
361 #endif // QT_NO_MENUBAR
362 
364 
366 
367 #endif // QMENUBAR_H
#define QT_MOC_COMPAT
Definition: qglobal.h:1128
The QKeyEvent class describes a key event.
Definition: qevent.h:224
virtual int heightForWidth(int) const
Returns the preferred height for this widget, given the width w.
Definition: qwidget.cpp:10241
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
virtual QSize minimumSizeHint() const
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
The QStyleOptionMenuItem class is used to describe the parameter necessary for drawing a menu item...
Definition: qstyleoption.h:435
ActionEvent
This enum type is used when calling QAction::activate()
Definition: qaction.h:176
virtual void mouseReleaseEvent(QMouseEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
Definition: qwidget.cpp:9286
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
#define at(className, varName)
virtual void mousePressEvent(QMouseEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
Definition: qwidget.cpp:9261
virtual void leaveEvent(QEvent *)
This event handler can be reimplemented in a subclass to receive widget leave events which are passed...
Definition: qwidget.cpp:9491
virtual void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
Definition: qobject.cpp:1294
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
virtual void resizeEvent(QResizeEvent *)
This event handler can be reimplemented in a subclass to receive widget resize events which are passe...
Definition: qwidget.cpp:9587
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QActionEvent class provides an event that is generated when a QAction is added, removed, or changed.
Definition: qevent.h:632
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
void addAction(QAction *action)
Appends the action action to this widget&#39;s list of actions.
Definition: qwidget.cpp:3317
virtual void focusOutEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) f...
Definition: qwidget.cpp:9457
virtual QSize sizeHint() const
void setWhatsThis(const QString &)
Definition: qwidget.cpp:11655
virtual void paintEvent(QPaintEvent *)
This event handler can be reimplemented in a subclass to receive paint events passed in event...
Definition: qwidget.cpp:9548
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
friend class QPixmap
Definition: qwidget.h:748
void getContentsMargins(int *left, int *top, int *right, int *bottom) const
Returns the widget&#39;s contents margins for left, top, right, and bottom.
Definition: qwidget.cpp:7509
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void setContentsMargins(int left, int top, int right, int bottom)
Sets the margins around the contents of the widget to have the sizes left, top, right, and bottom.
Definition: qwidget.cpp:7449
const char * name
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qwidget.cpp:9170
The QResizeEvent class contains event parameters for resize events.
Definition: qevent.h:349
unsigned int uint
Definition: qglobal.h:996
virtual void actionEvent(QActionEvent *)
This event handler is called with the given event whenever the widget&#39;s actions are changed...
Definition: qwidget.cpp:9600
virtual void keyPressEvent(QKeyEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
Definition: qwidget.cpp:9375
#define Q_OBJECT
Definition: qobjectdefs.h:157
virtual void setVisible(bool visible)
Definition: qwidget.cpp:7991
MenuRef OSMenuRef
virtual bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
Definition: qobject.cpp:1375
Corner
Definition: qnamespace.h:1456
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
The QMenuBar class provides a horizontal menu bar.
Definition: qmenubar.h:62
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the list, searching forward from index...
Definition: qlist.h:847
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
void removeAction(QAction *action)
Removes the action action from this widget&#39;s list of actions.
Definition: qwidget.cpp:3386
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QList< QAction * > actions() const
Returns the (possibly empty) list of this widget&#39;s actions.
Definition: qwidget.cpp:3407
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
struct OpaqueMenuRef * MenuRef
bool defaultUp
the popup orientation
Definition: qmenubar.h:66
quint16 index
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
bool nativeMenuBar
Whether or not a menubar will be used as a native menubar on platforms that support it...
Definition: qmenubar.h:67
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
bool qt_mac_activate_action(MenuRef menu, uint command, QAction::ActionEvent action_e, bool by_accel)
Definition: qmenu_mac.mm:209
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
virtual void mouseMoveEvent(QMouseEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events ...
Definition: qwidget.cpp:9239
The QWindowsStyle class provides a Microsoft Windows-like look and feel.
Definition: qwindowsstyle.h:57
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
QString whatsThis() const
#define QT_END_HEADER
Definition: qglobal.h:137
virtual void focusInEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
Definition: qwidget.cpp:9431
The QFocusEvent class contains event parameters for widget focus events.
Definition: qevent.h:275
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
#define text
Definition: qobjectdefs.h:80
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60