📄 qmenubar.cpp
字号:
The ampersand in the menu item's text sets Alt+F as a shortcut for this menu. (You can use "\&\&" to get a real ampersand in the menu bar.) There is no need to lay out a menu bar. It automatically sets its own geometry to the top of the parent widget and changes it appropriately whenever the parent is resized. \omit Example of creating a menu bar with menu items (from \l menu/menu.cpp): \quotefile menu/menu.cpp \skipto file = new QMenu \printline \skipto Qt::Key_O \printline \printline \skipto new QMenuBar \printline \skipto addMenu \printline \endomit In most main window style applications you would use the menuBar() provided in QMainWindow, adding \l{QMenu}s to the menu bar and adding \l{QAction}s to the popup menus. Example (from the \l{mainwindows/menus}{Menus} example): \quotefile mainwindows/menus/mainwindow.cpp \skipto fileMenu = \printuntil fileMenu->addAction( Menu items may be removed with removeAction(). \image motif-menubar.png A menubar shown in the Motif widget style. \image plastique-menubar.png A menubar shown in the Plastique widget style. \section1 QMenuBar on Qt/Mac QMenuBar on Qt/Mac is a wrapper for using the system-wide menubar. If you have multiple menubars in one dialog the outermost menubar (normally inside a widget with widget flag Qt::Window) will be used for the system-wide menubar. Qt/Mac also provides a menubar merging feature to make QMenuBar conform more closely to accepted Mac OS X menubar layout. The merging functionality is based on string matching the title of a QMenu entry. These strings are translated (using QObject::tr()) in the "QMenuBar" context. If an entry is moved its slots will still fire as if it was in the original place. The table below outlines the strings looked for and where the entry is placed if matched: \table \header \i String matches \i Placement \i Notes \row \i about.* \i Application Menu | About <application name> \i If this entry is not found no About item will appear in the Application Menu \row \i config, options, setup, settings or preferences \i Application Menu | Preferences \i If this entry is not found the Settings item will be disabled \row \i quit or exit \i Application Menu | Quit <application name> \i If this entry is not found a default Quit item will be created to call QApplication::quit() \endtable The \l{mainwindows/menus}{Menus} example shows how to use QMenuBar and QMenu. \sa QMenu, QShortcut, QAction, {http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuidelines/index.html}{Introduction to Apple Human Interface Guidelines} {fowler}{GUI Design Handbook: Menu Bar}*/void QMenuBarPrivate::init(){ Q_Q(QMenuBar); q->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum); q->setAttribute(Qt::WA_CustomWhatsThis);#ifdef Q_WS_MAC macCreateMenuBar(q->parentWidget()); if(mac_menubar) q->hide();#endif q->setBackgroundRole(QPalette::Button); q->setAutoFillBackground(true); oldWindow = oldParent = 0;#ifdef QT3_SUPPORT doAutoResize = false;#endif handleReparent(); q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, q)); extension = new QMenuBarExtension(q); extension->setFocusPolicy(Qt::NoFocus); extension->hide();}/*! Constructs a menu bar with parent \a parent.*/QMenuBar::QMenuBar(QWidget *parent) : QWidget(*new QMenuBarPrivate, parent, 0){ Q_D(QMenuBar); d->init();}#ifdef QT3_SUPPORT/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QMenuBar::QMenuBar(QWidget *parent, const char *name) : QWidget(*new QMenuBarPrivate, parent, 0){ Q_D(QMenuBar); d->init(); setObjectName(QString::fromAscii(name));}#endif/*! Destroys the menu bar.*/QMenuBar::~QMenuBar(){#ifdef Q_WS_MAC Q_D(QMenuBar); d->macDestroyMenuBar();#endif}/*! \overload This convenience function creates a new action with \a text. The function adds the newly created action to the menu's list of actions, and returns it. \sa QWidget::addAction()*/QAction *QMenuBar::addAction(const QString &text){ QAction *ret = new QAction(text, this); addAction(ret); return ret;}/*! \overload This convenience function creates a new action with the given \a text. The action's triggered() signal is connected to the \a receiver's \a member slot. The function adds the newly created action to the menu's list of actions and returns it. \sa QWidget::addAction()*/QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char* member){ QAction *ret = new QAction(text, this); QObject::connect(ret, SIGNAL(triggered()), receiver, member); addAction(ret); return ret;}/*! Appends a new QMenu with \a title to the menubar. The menubar takes ownership of the menu. Returns the new menu. \sa QWidget::addAction() QMenu::menuAction()*/QMenu *QMenuBar::addMenu(const QString &title){ QMenu *menu = new QMenu(title, this); addAction(menu->menuAction()); return menu;}/*! Appends a new QMenu with \a icon and \a title to the menubar. The menubar takes ownership of the menu. Returns the new menu. \sa QWidget::addAction() QMenu::menuAction()*/QMenu *QMenuBar::addMenu(const QIcon &icon, const QString &title){ QMenu *menu = new QMenu(title, this); menu->setIcon(icon); addAction(menu->menuAction()); return menu;}/*! Appends \a menu to the menubar. Returns the menu's menuAction(). \sa QWidget::addAction() QMenu::menuAction()*/QAction *QMenuBar::addMenu(QMenu *menu){ QAction *action = menu->menuAction(); addAction(action); return action;}/*! Appends a separator to the menu.*/QAction *QMenuBar::addSeparator(){ QAction *ret = new QAction(this); ret->setSeparator(true); addAction(ret); return ret;}/*! This convenience function inserts \a menu before action \a before and returns the menus menuAction(). \sa QWidget::insertAction() addMenu()*/QAction *QMenuBar::insertMenu(QAction *before, QMenu *menu){ QAction *action = menu->menuAction(); insertAction(before, action); return action;}/*! Returns the QAction that is currently highlighted. A null pointer will be returned if no action is currently selected.*/QAction *QMenuBar::activeAction() const{ Q_D(const QMenuBar); return d->currentAction;}/*! \since 4.1 Sets the currently highlighted action to \a act.*/void QMenuBar::setActiveAction(QAction *act){ Q_D(QMenuBar); d->setCurrentAction(act, true, false);}/*! Removes all the actions from the menu bar. \sa removeAction()*/void QMenuBar::clear(){ QList<QAction*> acts = actions(); for(int i = 0; i < acts.size(); i++) removeAction(acts[i]);}/*! \property QMenuBar::defaultUp \brief the popup orientation The default popup orientation. By default, menus pop "down" the screen. By setting the property to true, the menu will pop "up". You might call this for menus that are \e below the document to which they refer. If the menu would not fit on the screen, the other direction is used automatically.*/void QMenuBar::setDefaultUp(bool b){ Q_D(QMenuBar); d->defaultPopDown = !b;}bool QMenuBar::isDefaultUp() const{ Q_D(const QMenuBar); return !d->defaultPopDown;}/*! \reimp*/void QMenuBar::resizeEvent(QResizeEvent *){ Q_D(QMenuBar); d->itemsDirty = true; d->updateGeometries();}/*! \reimp*/void QMenuBar::paintEvent(QPaintEvent *e){ Q_D(QMenuBar); QPainter p(this); QRegion emptyArea(rect()); //draw the items for (int i = 0; i < d->actionList.count(); ++i) { QAction *action = d->actionList.at(i); QRect adjustedActionRect = d->actionRect(action); if (adjustedActionRect.isEmpty() || !d->isVisible(action)) continue; if(!e->rect().intersects(adjustedActionRect)) continue; emptyArea -= adjustedActionRect; QStyleOptionMenuItem opt = d->getStyleOption(action); opt.rect = adjustedActionRect; p.setClipRect(adjustedActionRect); style()->drawControl(QStyle::CE_MenuBarItem, &opt, &p, this); } //draw border if(int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this)) { QRegion borderReg; borderReg += QRect(0, 0, fw, height()); //left borderReg += QRect(width()-fw, 0, fw, height()); //right borderReg += QRect(0, 0, width(), fw); //top borderReg += QRect(0, height()-fw, width(), fw); //bottom p.setClipRegion(borderReg); emptyArea -= borderReg; QStyleOptionFrame frame; frame.rect = rect(); frame.palette = palette(); frame.state = QStyle::State_None; frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth); frame.midLineWidth = 0; style()->drawPrimitive(QStyle::PE_PanelMenuBar, &frame, &p, this); } p.setClipRegion(emptyArea); QStyleOptionMenuItem menuOpt; menuOpt.palette = palette(); menuOpt.state = QStyle::State_None; menuOpt.menuItemType = QStyleOptionMenuItem::EmptyArea; menuOpt.checkType = QStyleOptionMenuItem::NotCheckable; menuOpt.rect = rect(); menuOpt.menuRect = rect(); style()->drawControl(QStyle::CE_MenuBarEmptyArea, &menuOpt, &p, this);}/*! \reimp*/void QMenuBar::mousePressEvent(QMouseEvent *e){ Q_D(QMenuBar); if(e->button() != Qt::LeftButton) return; QAction *action = d->actionAt(e->pos()); if (!action || !d->isVisible(action)) { d->setCurrentAction(0);#ifndef QT_NO_WHATSTHIS if (QWhatsThis::inWhatsThisMode()) QWhatsThis::showText(e->globalPos(), d->whatsThis, this);#endif return; } d->mouseDown = true; if(d->currentAction == action && d->popupState) { if((d->closePopupMode = style()->styleHint(QStyle::SH_MenuBar_DismissOnSecondClick))) update(d->actionRect(action)); } else { d->setCurrentAction(action, true); }}/*! \reimp*/void QMenuBar::mouseReleaseEvent(QMouseEvent *e){ Q_D(QMenuBar); if(e->button() != Qt::LeftButton || !d->mouseDown) return; d->mouseDown = false; QAction *action = d->actionAt(e->pos()); if((d->closePopupMode && action == d->currentAction) || !action || !action->menu()) { if(action) d->activateAction(action, QAction::Trigger); d->setCurrentAction(action, false); } d->closePopupMode = 0;}/*! \reimp*/void QMenuBar::keyPressEvent(QKeyEvent *e){ Q_D(QMenuBar); int key = e->key(); if(isRightToLeft()) { // in reverse mode open/close key for submenues are reversed if(key == Qt::Key_Left) key = Qt::Key_Right; else if(key == Qt::Key_Right) key = Qt::Key_Left; } if(key == Qt::Key_Tab) //means right key = Qt::Key_Right; else if(key == Qt::Key_Backtab) //means left key = Qt::Key_Left; bool key_consumed = false; switch(key) { case Qt::Key_Up: case Qt::Key_Down: case Qt::Key_Enter: case Qt::Key_Space: case Qt::Key_Return: { if(!style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this) || !d->currentAction) break; if(d->currentAction->menu()) { d->popupAction(d->currentAction, true); } else if(key == Qt::Key_Enter || key == Qt::Key_Return || key == Qt::Key_Space) { d->activateAction(d->currentAction, QAction::Trigger); d->setCurrentAction(d->currentAction, false); } key_consumed = true; break; } case Qt::Key_Right: case Qt::Key_Left: { if(d->currentAction) { QAction *nextAction = 0; for(int i=0; i<(int)d->actionList.count(); i++) { if(d->actionList.at(i) == (QAction*)d->currentAction) { if(key == Qt::Key_Left) { if(i > 0) nextAction = d->actionList.at(i-1); } else { if(i < d->actionList.count()-1) nextAction = d->actionList.at(i+1); } break; } } if(!nextAction) { if(key == Qt::Key_Left) nextAction = d->actionList.last(); else nextAction = d->actionList.first(); } if(nextAction) { d->setCurrentAction(nextAction, d->popupState, true); key_consumed = true; } } break; } case Qt::Key_Escape: d->setCurrentAction(0); d->setKeyboardMode(false); key_consumed = true; break; default: key_consumed = false; } if(!key_consumed && (!e->modifiers() || (e->modifiers()&(Qt::MetaModifier|Qt::AltModifier))) && e->text().length()==1 && !d->popupState) { int clashCount = 0; QAction *first = 0, *currentSelected = 0, *firstAfterCurrent = 0; { QChar c = e->text()[0].toUpper(); for(int i = 0; i < d->actionList.size(); ++i) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -