📄 qmenubar.cpp
字号:
max_item_height = qMax(max_item_height, sz.height()); //append actionRects.insert(action, QRect(0, 0, sz.width(), sz.height())); actionList.append(action); } } //calculate position int x = ((start == -1) ? hmargin : start) + itemSpacing; int y = vmargin; for(int i = 0; i < actionList.count(); i++) { QAction *action = actionList.at(i); QRect &rect = actionRects[action]; //resize rect.setHeight(max_item_height); //move if(separator != -1 && i >= separator) { //after the separator int left = (max_width - separator_len - hmargin - itemSpacing) + (x - separator_start - hmargin); if(left < separator_start) { //wrap separator_start = x = hmargin; y += max_item_height; } rect.moveLeft(left); } else { rect.moveLeft(x); } rect.moveTop(y); //keep moving along.. x += rect.width() + itemSpacing; }}void QMenuBarPrivate::activateAction(QAction *action, QAction::ActionEvent action_e){ Q_Q(QMenuBar); if (!action || !action->isEnabled()) return; action->activate(action_e); if (action_e == QAction::Hover) action->showStatusText(q);// if(action_e == QAction::Trigger)// emit q->activated(action);// else if(action_e == QAction::Hover)// emit q->highlighted(action);}void QMenuBarPrivate::_q_actionTriggered(){ Q_Q(QMenuBar); if (QAction *action = qobject_cast<QAction *>(q->sender())) { emit q->triggered(action);#ifdef QT3_SUPPORT emit q->activated(q->findIdForAction(action));#endif }}void QMenuBarPrivate::_q_actionHovered(){ Q_Q(QMenuBar); if (QAction *action = qobject_cast<QAction *>(q->sender())) { emit q->hovered(action);#ifndef QT_NO_ACCESSIBILITY if (QAccessible::isActive()) { QList<QAction*> actions = q->actions(); int actionIndex = actions.indexOf(action); ++actionIndex; QAccessible::updateAccessibility(q, actionIndex, QAccessible::Focus); QAccessible::updateAccessibility(q, actionIndex, QAccessible::Selection); }#endif //QT_NO_ACCESSIBILITY#ifdef QT3_SUPPORT emit q->highlighted(q->findIdForAction(action));#endif }}/*! Initialize \a option with the values from the menu bar and information from \a action. This method is useful for subclasses when they need a QStyleOptionMenuItem, but don't want to fill in all the information themselves. \sa QStyleOption::initFrom() QMenu::initStyleOption()*/void QMenuBar::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const{ if (!option || !action) return; Q_D(const QMenuBar); option->palette = palette(); option->state = QStyle::State_None; if (isEnabled() && action->isEnabled()) option->state |= QStyle::State_Enabled; else option->palette.setCurrentColorGroup(QPalette::Disabled); option->fontMetrics = fontMetrics(); if (d->currentAction && d->currentAction == action) { option->state |= QStyle::State_Selected; if (d->popupState && !d->closePopupMode) option->state |= QStyle::State_Sunken; } if (hasFocus() || d->currentAction) option->state |= QStyle::State_HasFocus; option->menuRect = rect(); option->menuItemType = QStyleOptionMenuItem::Normal; option->checkType = QStyleOptionMenuItem::NotCheckable; option->text = action->text(); option->icon = action->icon();}/*! \class QMenuBar \brief The QMenuBar class provides a horizontal menu bar. \ingroup application \mainclass A menu bar consists of a list of pull-down menu items. You add menu items with addMenu(). For example, asuming that \c menubar is a pointer to a QMenuBar and \c fileMenu is a pointer to a QMenu, the following statement inserts the menu into the menu bar: \code menubar->addMenu(fileMenu); \endcode 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(). \section1 Platform Dependent Look and Feel Different platforms have different requirements for the appearance of menu bars and their behavior when the user interacts with them. For example, Windows systems are often configured so that the underlined character mnemonics that indicate keyboard shortcuts for items in the menu bar are only shown when the \gui{Alt} key is pressed. \table \row \o \inlineimage plastique-menubar.png A menu bar shown in the Plastique widget style. \o The \l{QPlastiqueStyle}{Plastique widget style}, like most other styles, handles the \gui{Help} menu in the same way as it handles any other menu. \row \o \inlineimage motif-menubar.png A menu bar shown in the Motif widget style. \o The \l{QMotifStyle}{Motif widget style} treats \gui{Help} menus in a special way, placing them at right-hand end of the menu bar. \endtable \section1 QMenuBar on Qt/Mac QMenuBar on Qt/Mac is a wrapper for using the system-wide menu bar. If you have multiple menu bars in one dialog the outermost menu bar (normally inside a widget with widget flag Qt::Window) will be used for the system-wide menu bar. Qt/Mac also provides a menu bar merging feature to make QMenuBar conform more closely to accepted Mac OS X menu bar 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 You can override this behavior by using the QAction::menuRole() property. If you wish to make all windows in a Mac application share the same menu bar, you need to create a menu bar that does not have a parent. The menu bar is created like this: \code QMenuBar *menuBar = new QMenuBar(0); \endcode \bold{Note:} The text used for the application name in the menu bar is obtained from the value set in the \c{Info.plist} file in the application's bundle. See \l{Deploying an Application on Qt/Mac} for more information. \section1 Examples The \l{mainwindows/menus}{Menus} example shows how to use QMenuBar and QMenu. The other \l{Qt Examples#Main Windows}{main window application examples} also provide menus using these classes. \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}, {Menus Example}*/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); 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(bool)), receiver, member); addAction(ret); return ret;}/*! Appends a new QMenu with \a title to the menu bar. The menu bar 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 menu bar. The menu bar 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 menu bar. 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 creates a new separator action, i.e. an action with QAction::isSeparator() returning true. The function inserts the newly created action into this menu bar's list of actions before action \a before and returns it. \sa QWidget::insertAction(), addSeparator()*/QAction *QMenuBar::insertSeparator(QAction *before){ QAction *action = new QAction(this); action->setSeparator(true); insertAction(before, action); return action;}/*! 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -