- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qmenu.h

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    00004 ** All rights reserved.
    00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
    00006 **
    00007 ** This file is part of the QtGui module of the Qt Toolkit.
    00008 **
    00009 ** $QT_BEGIN_LICENSE:LGPL$
    00010 ** Commercial Usage
    00011 ** Licensees holding valid Qt Commercial licenses may use this file in
    00012 ** accordance with the Qt Commercial License Agreement provided with the
    00013 ** Software or, alternatively, in accordance with the terms contained in
    00014 ** a written agreement between you and Nokia.
    00015 **
    00016 ** GNU Lesser General Public License Usage
    00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
    00018 ** General Public License version 2.1 as published by the Free Software
    00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
    00020 ** packaging of this file.  Please review the following information to
    00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00023 **
    00024 ** In addition, as a special exception, Nokia gives you certain additional
    00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
    00027 **
    00028 ** GNU General Public License Usage
    00029 ** Alternatively, this file may be used under the terms of the GNU
    00030 ** General Public License version 3.0 as published by the Free Software
    00031 ** Foundation and appearing in the file LICENSE.GPL included in the
    00032 ** packaging of this file.  Please review the following information to
    00033 ** ensure the GNU General Public License version 3.0 requirements will be
    00034 ** met: http://www.gnu.org/copyleft/gpl.html.
    00035 **
    00036 ** If you have questions regarding the use of this file, please contact
    00037 ** Nokia at qt-info@nokia.com.
    00038 ** $QT_END_LICENSE$
    00039 **
    00040 ****************************************************************************/
    00041
    00042 #ifndef QMENU_H
    00043 #define QMENU_H
    00044
    00045 #include <QtGui/qwidget.h>
    00046 #include <QtCore/qstring.h>
    00047 #include <QtGui/qicon.h>
    00048 #include <QtGui/qaction.h>
    00049
    00050 #ifdef QT3_SUPPORT
    00051 #include <QtGui/qpixmap.h>
    00052 #endif
    00053
    00054 QT_BEGIN_HEADER
    00055
    00056 QT_BEGIN_NAMESPACE
    00057
    00058 QT_MODULE(Gui)
    00059
    00060 #ifndef QT_NO_MENU
    00061
    00062 class QMenuPrivate;
    00063 class QStyleOptionMenuItem;
    00064 #ifdef QT3_SUPPORT
    00065 class QMenuItem;
    00066 #endif
    00067
    00068 class Q_GUI_EXPORT QMenu : public QWidget
    00069 {
    00070 private:
    00071     Q_OBJECT
    00072     Q_DECLARE_PRIVATE(QMenu)
    00073
    00074     Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
    00075     Q_PROPERTY(QString title READ title WRITE setTitle)
    00076     Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
    00077     Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
    00078
    00079 public:
    00080     explicit QMenu(QWidget *parent = 0);
    00081     explicit QMenu(const QString &title, QWidget *parent = 0);
    00082     ~QMenu();
    00083
    00084 #ifdef Q_NO_USING_KEYWORD
    00085     inline void addAction(QAction *action) { QWidget::addAction(action); }
    00086 #else
    00087     using QWidget::addAction;
    00088 #endif
    00089     QAction *addAction(const QString &text);
    00090     QAction *addAction(const QIcon &icon, const QString &text);
    00091     QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
    00092     QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
    00093
    00094     QAction *addMenu(QMenu *menu);
    00095     QMenu *addMenu(const QString &title);
    00096     QMenu *addMenu(const QIcon &icon, const QString &title);
    00097
    00098     QAction *addSeparator();
    00099
    00100     QAction *insertMenu(QAction *before, QMenu *menu);
    00101     QAction *insertSeparator(QAction *before);
    00102
    00103     bool isEmpty() const;
    00104     void clear();
    00105
    00106     void setTearOffEnabled(bool);
    00107     bool isTearOffEnabled() const;
    00108
    00109     bool isTearOffMenuVisible() const;
    00110     void hideTearOffMenu();
    00111
    00112     void setDefaultAction(QAction *);
    00113     QAction *defaultAction() const;
    00114
    00115     void setActiveAction(QAction *act);
    00116     QAction *activeAction() const;
    00117
    00118     void popup(const QPoint &pos, QAction *at=0);
    00119     QAction *exec();
    00120     QAction *exec(const QPoint &pos, QAction *at=0);
    00121
    00122     // ### Qt 5: merge
    00123     static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at=0);
    00124     static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at, QWidget *parent);
    00125
    00126     QSize sizeHint() const;
    00127
    00128     QRect actionGeometry(QAction *) const;
    00129     QAction *actionAt(const QPoint &) const;
    00130
    00131     QAction *menuAction() const;
    00132
    00133     QString title() const;
    00134     void setTitle(const QString &title);
    00135
    00136     QIcon icon() const;
    00137     void setIcon(const QIcon &icon);
    00138
    00139     void setNoReplayFor(QWidget *widget);
    00140 #ifdef Q_WS_MAC
    00141     OSMenuRef macMenu(OSMenuRef merge=0);
    00142 #endif
    00143
    00144 #ifdef Q_WS_WINCE
    00145     HMENU wceMenu();
    00146 #endif
    00147
    00148     bool separatorsCollapsible() const;
    00149     void setSeparatorsCollapsible(bool collapse);
    00150
    00151 Q_SIGNALS:
    00152     void aboutToShow();
    00153     void aboutToHide();
    00154     void triggered(QAction *action);
    00155     void hovered(QAction *action);
    00156
    00157 protected:
    00158     int columnCount() const;
    00159
    00160     void changeEvent(QEvent *);
    00161     void keyPressEvent(QKeyEvent *);
    00162     void mouseReleaseEvent(QMouseEvent *);
    00163     void mousePressEvent(QMouseEvent *);
    00164     void mouseMoveEvent(QMouseEvent *);
    00165 #ifndef QT_NO_WHEELEVENT
    00166     void wheelEvent(QWheelEvent *);
    00167 #endif
    00168     void enterEvent(QEvent *);
    00169     void leaveEvent(QEvent *);
    00170     void hideEvent(QHideEvent *);
    00171     void paintEvent(QPaintEvent *);
    00172     void actionEvent(QActionEvent *);
    00173     void timerEvent(QTimerEvent *);
    00174     bool event(QEvent *);
    00175     bool focusNextPrevChild(bool next);
    00176     void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
    00177
    00178 #ifdef Q_WS_WINCE
    00179     QAction* wceCommands(uint command);
    00180 #endif
    00181
    00182 private Q_SLOTS:
    00183     void internalSetSloppyAction();
    00184     void internalDelayedPopup();
    00185
    00186 private:
    00187     Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
    00188     Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
    00189     Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
    00190
    00191 #ifdef QT3_SUPPORT
    00192 public:
    00193     //menudata
    00194     inline QT3_SUPPORT uint count() const { return actions().count(); }
    00195     inline QT3_SUPPORT int insertItem(const QString &text, const QObject *receiver, const char* member,
    00196                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
    00197         return insertAny(0, &text, receiver, member, &shortcut, 0, id, index);
    00198     }
    00199     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text,
    00200                                     const QObject *receiver, const char* member,
    00201                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
    00202         return insertAny(&icon, &text, receiver, member, &shortcut, 0, id, index);
    00203     }
    00204     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member,
    00205                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
    00206         QIcon icon(pixmap);
    00207         return insertAny(&icon, 0, receiver, member, &shortcut, 0, id, index);
    00208     }
    00209     inline QT3_SUPPORT int insertItem(const QString &text, int id=-1, int index=-1) {
    00210         return insertAny(0, &text, 0, 0, 0, 0, id, index);
    00211     }
    00212     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, int id=-1, int index=-1) {
    00213         return insertAny(&icon, &text, 0, 0, 0, 0, id, index);
    00214     }
    00215     inline QT3_SUPPORT int insertItem(const QString &text, QMenu *popup, int id=-1, int index=-1) {
    00216         return insertAny(0, &text, 0, 0, 0, popup, id, index);
    00217     }
    00218     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id=-1, int index=-1) {
    00219         return insertAny(&icon, &text, 0, 0, 0, popup, id, index);
    00220     }
    00221     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, int id=-1, int index=-1) {
    00222         QIcon icon(pixmap);
    00223         return insertAny(&icon, 0, 0, 0, 0, 0, id, index);
    00224     }
    00225     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, QMenu *popup, int id=-1, int index=-1) {
    00226         QIcon icon(pixmap);
    00227         return insertAny(&icon, 0, 0, 0, 0, popup, id, index);
    00228     }
    00229     QT3_SUPPORT int insertItem(QMenuItem *item, int id=-1, int index=-1);
    00230     QT3_SUPPORT int insertSeparator(int index=-1);
    00231     inline QT3_SUPPORT void removeItem(int id) {
    00232         if(QAction *act = findActionForId(id))
    00233             removeAction(act); }
    00234     inline QT3_SUPPORT void removeItemAt(int index) {
    00235         if(QAction *act = actions().value(index))
    00236             removeAction(act); }
    00237 #ifndef QT_NO_SHORTCUT
    00238     inline QT3_SUPPORT QKeySequence accel(int id) const {
    00239         if(QAction *act = findActionForId(id))
    00240             return act->shortcut();
    00241         return QKeySequence(); }
    00242     inline QT3_SUPPORT void setAccel(const QKeySequence& key, int id) {
    00243         if(QAction *act = findActionForId(id))
    00244             act->setShortcut(key);
    00245     }
    00246 #endif
    00247     inline QT3_SUPPORT QIcon iconSet(int id) const {
    00248         if(QAction *act = findActionForId(id))
    00249             return act->icon();
    00250         return QIcon(); }
    00251     inline QT3_SUPPORT QString text(int id) const {
    00252         if(QAction *act = findActionForId(id))
    00253             return act->text();
    00254         return QString(); }
    00255     inline QT3_SUPPORT QPixmap pixmap(int id) const {
    00256         if(QAction *act = findActionForId(id))
    00257             return act->icon().pixmap(QSize(22, 22));
    00258         return QPixmap(); }
    00259     inline QT3_SUPPORT void setWhatsThis(int id, const QString &w) {
    00260         if(QAction *act = findActionForId(id))
    00261             act->setWhatsThis(w); }
    00262     inline QT3_SUPPORT QString whatsThis(int id) const {
    00263         if(QAction *act = findActionForId(id))
    00264             return act->whatsThis();
    00265         return QString(); }
    00266
    00267     inline QT3_SUPPORT void changeItem(int id, const QString &text) {
    00268         if(QAction *act = findActionForId(id))
    00269             act->setText(text); }
    00270     inline QT3_SUPPORT void changeItem(int id, const QPixmap &pixmap) {
    00271         if(QAction *act = findActionForId(id))
    00272             act->setIcon(QIcon(pixmap)); }
    00273     inline QT3_SUPPORT void changeItem(int id, const QIcon &icon, const QString &text) {
    00274         if(QAction *act = findActionForId(id)) {
    00275             act->setIcon(icon);
    00276             act->setText(text);
    00277         }
    00278     }
    00279     inline QT3_SUPPORT void setActiveItem(int id) {
    00280         setActiveAction(findActionForId(id));
    00281     }
    00282     inline QT3_SUPPORT bool isItemActive(int id) const {
    00283         return findActionForId(id) == activeAction();
    00284     }
    00285     inline QT3_SUPPORT bool isItemEnabled(int id) const {
    00286         if(QAction *act = findActionForId(id))
    00287             return act->isEnabled();
    00288         return false; }
    00289     inline QT3_SUPPORT void setItemEnabled(int id, bool enable) {
    00290         if(QAction *act = findActionForId(id))
    00291             act->setEnabled(enable);
    00292     }
    00293     inline QT3_SUPPORT bool isItemChecked(int id) const {
    00294         if(QAction *act = findActionForId(id))
    00295             return act->isChecked();
    00296         return false;
    00297     }
    00298     inline QT3_SUPPORT void setItemChecked(int id, bool check) {
    00299         if(QAction *act = findActionForId(id)) {
    00300             act->setCheckable(true);
    00301             act->setChecked(check);
    00302         }
    00303     }
    00304     inline QT3_SUPPORT bool isItemVisible(int id) const {
    00305         if(QAction *act = findActionForId(id))
    00306             return act->isVisible();
    00307         return false;
    00308     }
    00309     inline QT3_SUPPORT void setItemVisible(int id, bool visible) {
    00310         if(QAction *act = findActionForId(id))
    00311             act->setVisible(visible);
    00312     }
    00313     inline QT3_SUPPORT QRect itemGeometry(int index) {
    00314         if(QAction *act = actions().value(index))
    00315             return actionGeometry(act);
    00316         return QRect();
    00317     }
    00318     inline QT3_SUPPORT QFont itemFont(int id) const {
    00319         if(QAction *act = findActionForId(id))
    00320             return act->font();
    00321         return QFont();
    00322     }
    00323     inline QT3_SUPPORT void setItemFont(int id, const QFont &font) {
    00324         if(QAction *act = findActionForId(id))
    00325             act->setFont(font);
    00326     }
    00327     inline QT3_SUPPORT int indexOf(int id) const {
    00328         return actions().indexOf(findActionForId(id));
    00329     }
    00330     inline QT3_SUPPORT int idAt(int index) const {
    00331         return findIdForAction(actions().value(index));
    00332     }
    00333     QT3_SUPPORT void setId (int index, int id);
    00334     inline QT3_SUPPORT void activateItemAt(int index) {
    00335         if(QAction *ret = actions().value(index))
    00336             ret->activate(QAction::Trigger);
    00337     }
    00338     inline QT3_SUPPORT bool connectItem(int id, const QObject *receiver, const char* member) {
    00339         if(QAction *act = findActionForId(id)) {
    00340             QObject::connect(act, SIGNAL(activated(int)), receiver, member);
    00341             return true;
    00342         }
    00343         return false;
    00344     }
    00345     inline QT3_SUPPORT bool disconnectItem(int id,const QObject *receiver, const char* member) {
    00346         if(QAction *act = findActionForId(id)) {
    00347             QObject::disconnect(act, SIGNAL(triggered()), receiver, member);
    00348             return true;
    00349         }
    00350         return false;
    00351     }
    00352     inline QT3_SUPPORT QMenuItem *findItem(int id) const {
    00353         return reinterpret_cast<QMenuItem*>(findActionForId(id));
    00354     }
    00355
    00356     inline QT3_SUPPORT void setCheckable(bool){}
    00357     inline QT3_SUPPORT bool isCheckable() const {return true;}
    00358
    00359     QT3_SUPPORT QMenuItem *findPopup( QMenu *popup, int *index );
    00360
    00361     QT3_SUPPORT bool setItemParameter(int id, int param);
    00362     QT3_SUPPORT int itemParameter(int id) const;
    00363
    00364     //frame
    00365     QT3_SUPPORT int frameWidth() const;
    00366
    00367     //popupmenu
    00368     inline QT3_SUPPORT void popup(const QPoint & pos, int indexAtPoint) { popup(pos, actions().value(indexAtPoint)); }
    00369     inline QT3_SUPPORT int insertTearOffHandle(int = 0, int = 0) {
    00370         setTearOffEnabled(true);
    00371         return -1;
    00372     }
    00373
    00374 protected:
    00375     inline QT3_SUPPORT int itemAtPos(const QPoint &p, bool ignoreSeparator = true) {
    00376         QAction *ret = actionAt(p);
    00377         if(ignoreSeparator && ret && ret->isSeparator())
    00378             return -1;
    00379         return findIdForAction(ret);
    00380     }
    00381     inline QT3_SUPPORT int columns() const { return columnCount(); }
    00382     inline QT3_SUPPORT int itemHeight(int index) {
    00383         return actionGeometry(actions().value(index)).height();
    00384     }
    00385     inline QT3_SUPPORT int itemHeight(QMenuItem *mi) {
    00386         return actionGeometry(reinterpret_cast<QAction *>(mi)).height();
    00387     }
    00388
    00389 Q_SIGNALS:
    00390     QT_MOC_COMPAT void activated(int itemId);
    00391     QT_MOC_COMPAT void highlighted(int itemId);
    00392
    00393 private:
    00394     int insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
    00395                   const QKeySequence *shorcut, const QMenu *popup, int id, int index);
    00396     QAction *findActionForId(int id) const;
    00397     int findIdForAction(QAction*) const;
    00398 #endif
    00399
    00400 protected:
    00401     QMenu(QMenuPrivate &dd, QWidget* parent = 0);
    00402
    00403 private:
    00404     Q_DISABLE_COPY(QMenu)
    00405
    00406     friend class QMenuBar;
    00407     friend class QMenuBarPrivate;
    00408     friend class QTornOffMenu;
    00409     friend class Q3PopupMenu;
    00410     friend class QComboBox;
    00411     friend class QAction;
    00412     friend class QToolButtonPrivate;
    00413
    00414 #ifdef Q_WS_MAC
    00415     friend void qt_mac_trayicon_activate_action(QMenu *, QAction *action);
    00416     friend bool qt_mac_watchingAboutToShow(QMenu *);
    00417     friend OSStatus qt_mac_menu_event(EventHandlerCallRef, EventRef, void *);
    00418     friend bool qt_mac_activate_action(OSMenuRef, uint, QAction::ActionEvent, bool);
    00419     friend void qt_mac_emit_menuSignals(QMenu *, bool);
    00420     friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action);
    00421 #endif
    00422 };
    00423
    00424 #endif // QT_NO_MENU
    00425
    00426 QT_END_NAMESPACE
    00427
    00428 QT_END_HEADER
    00429
    00430 #endif // QMENU_H