📄 qmenu.cpp
字号:
opt.menuItemType = QStyleOptionMenuItem::Normal; opt.icon = action->icon(); QString textAndAccel = action->text();#ifndef QT_NO_SHORTCUT if (textAndAccel.indexOf('\t') == -1) { QKeySequence seq = action->shortcut(); if (!seq.isEmpty()) textAndAccel += '\t' + QString(seq); }#endif opt.text = textAndAccel; opt.tabWidth = tabWidth; opt.maxIconWidth = maxIconWidth; opt.menuRect = q->rect(); return opt;}/*! \class QMenu \brief The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus. \ingroup application \ingroup basic \mainclass A menu widget is a selection menu. It can be either a pull-down menu in a menu bar or a standalone context menu. Pull-down menus are shown by the menu bar when the user clicks on the respective item or presses the specified shortcut key. Use QMenuBar::addMenu() to insert a menu into a menu bar. Context menus are usually invoked by some special keyboard key or by right-clicking. They can be executed either asynchronously with popup() or synchronously with exec(). Menus can also be invoked in response to button presses; these are just like context menus except for how they are invoked. A menu consists of a list of action items. Actions are added with addAction(). An action is represented vertically and rendered by QStyle. In addition, actions can have a text label, an optional icon drawn on the very left side, and shortcut key sequence such as "Ctrl+X". There are three kinds of action items: separators, actions that show a submenu, and actions that perform an action. Separators are inserted with addSeparator(). For submenus use addMenu(). All other items are considered action items. When inserting action items you usually specify a receiver and a slot. The receiver will be notifed whenever the item is \l{QAction::triggered()}{triggered()}. In addition, QMenu provides two signals, activated() and highlighted(), which signal the QAction that was triggered from the menu. You clear a menu with clear() and remove individual action items with removeAction(). A QMenu can also provide a tear-off menu. A tear-off menu is a top-level window that contains a copy of the menu. This makes it possible for the user to "tear off" frequently used menus and position them in a convenient place on the screen. If you want this functionality for a particular menu, insert a tear-off handle with setTearOffEnabled(). When using tear-off menus, bear in mind that the concept isn't typically used on Microsoft Windows so some users may not be familiar with it. Consider using a QToolBar instead. See the \l{mainwindows/menus}{Menus Example} for an example of how to use QMenuBar and QMenu in your application. Important inherited functions: addAction(), removeAction(), clear(), addSeparator(), and addMenu(). \sa QMenuBar, {fowler}{GUI Design Handbook: Menu, Drop-Down and Pop-Up}*//*! Constructs a menu with parent \a parent. Although a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed (as with any other QObject).*/QMenu::QMenu(QWidget *parent) : QWidget(*new QMenuPrivate, parent, Qt::Popup){ Q_D(QMenu);#ifndef QT_NO_WHATSTHIS setAttribute(Qt::WA_CustomWhatsThis);#endif setMouseTracking(style()->styleHint(QStyle::SH_Menu_MouseTracking, 0, this)); if (style()->styleHint(QStyle::SH_Menu_Scrollable, 0, this)) { d->scroll = new QMenuPrivate::QMenuScroller; d->scroll->scrollFlags = QMenuPrivate::QMenuScroller::ScrollNone; } d->menuAction = new QAction(this); d->menuAction->d_func()->menu = this;}/*! Constructs a menu with a \a title and a \a parent. Although a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed (as with any other QObject). \sa title*/QMenu::QMenu(const QString &title, QWidget *parent) : QWidget(*new QMenuPrivate, parent, Qt::Popup){ Q_D(QMenu);#ifndef QT_NO_WHATSTHIS setAttribute(Qt::WA_CustomWhatsThis);#endif setMouseTracking(style()->styleHint(QStyle::SH_Menu_MouseTracking)); if (style()->styleHint(QStyle::SH_Menu_Scrollable, 0, this)) { d->scroll = new QMenuPrivate::QMenuScroller; d->scroll->scrollFlags = QMenuPrivate::QMenuScroller::ScrollNone; } d->menuAction = new QAction(title, this); d->menuAction->d_func()->menu = this;}/*! Destroys the menu.*/QMenu::~QMenu(){ Q_D(QMenu); if (d->eventLoop) d->eventLoop->exit(); if (d->tornPopup) d->tornPopup->close();}/*! \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 *QMenu::addAction(const QString &text){ QAction *ret = new QAction(text, this); addAction(ret); return ret;}/*! \overload This convenience function creates a new action with an \a icon and some \a text. The function adds the newly created action to the menu's list of actions, and returns it. \sa QWidget::addAction()*/QAction *QMenu::addAction(const QIcon &icon, const QString &text){ QAction *ret = new QAction(icon, text, this); addAction(ret); return ret;}/*! \overload This convenience function creates a new action with the text \a text and an optional shortcut \a shortcut. The action's \l{QAction::triggered()}{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 *QMenu::addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut){ QAction *action = new QAction(text, this);#ifdef QT_NO_SHORTCUT Q_UNUSED(shortcut);#else action->setShortcut(shortcut);#endif QObject::connect(action, SIGNAL(triggered()), receiver, member); addAction(action); return action;}/*! \overload This convenience function creates a new action with an \a icon and some \a text and an optional shortcut \a shortcut. The action's \l{QAction::triggered()}{triggered()} signal is connected to the \a member slot of the \a receiver object. The function adds the newly created action to the menu's list of actions, and returns it. \sa QWidget::addAction()*/QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut){ QAction *action = new QAction(icon, text, this);#ifdef QT_NO_SHORTCUT Q_UNUSED(shortcut);#else action->setShortcut(shortcut);#endif QObject::connect(action, SIGNAL(triggered()), receiver, member); addAction(action); return action;}/*! This convenience function adds \a menu as a submenu to this menu. It returns the menus menuAction(). \sa QWidget::addAction() QMenu::menuAction()*/QAction *QMenu::addMenu(QMenu *menu){ QAction *action = menu->menuAction(); addAction(action); return action;}/*! Appends a new QMenu with \a title to the menu. The menu takes ownership of the menu. Returns the new menu. \sa QWidget::addAction() QMenu::menuAction()*/QMenu *QMenu::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. The menu takes ownership of the menu. Returns the new menu. \sa QWidget::addAction() QMenu::menuAction()*/QMenu *QMenu::addMenu(const QIcon &icon, const QString &title){ QMenu *menu = new QMenu(title, this); menu->setIcon(icon); addAction(menu->menuAction()); return menu;}/*! This convenience function creates a new separator action, i.e. an action with QAction::isSeparator() returning true, and adds the new action to this menu's list of actions. It returns the newly created action. \sa QWidget::addAction()*/QAction *QMenu::addSeparator(){ QAction *action = new QAction(this); action->setSeparator(true); addAction(action); return action;}/*! This convenience function inserts \a menu before action \a before and returns the menus menuAction(). \sa QWidget::insertAction(), addMenu()*/QAction *QMenu::insertMenu(QAction *before, QMenu *menu){ QAction *action = menu->menuAction(); insertAction(before, action); return action;}/*! 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's list of actions before action \a before and returns it. \sa QWidget::insertAction(), addSeparator()*/QAction *QMenu::insertSeparator(QAction *before){ QAction *action = new QAction(this); action->setSeparator(true); insertAction(before, action); return action;}/*! This will set the default action to \a act. The default action may have a visual queue depending on the current QStyle. A default action is usually meant to indicate what will defaultly happen on a drop, as shown in a context menu. \sa defaultAction()*/void QMenu::setDefaultAction(QAction *act){ d_func()->defaultAction = act;}/*! Returns the current default action. \sa setDefaultAction()*/QAction *QMenu::defaultAction() const{ return d_func()->defaultAction;}/*! \property QMenu::tearOffEnabled \brief whether the menu supports being torn off When true, QMenu has a special menu item (often shown as a dashed line at the top of the menu) that creates a copy of the menu when the tear-off menu item is triggered. This "torn-off" copy lives in a separate window. It contains the same menu items as the original menu, with the exception of the tear-off handle.*/void QMenu::setTearOffEnabled(bool b){ Q_D(QMenu); if (d->tearoff == b) return; if (!b && d->tornPopup) d->tornPopup->close(); d->tearoff = b; d->itemsDirty = true; if (isVisible()) resize(sizeHint());}bool QMenu::isTearOffEnabled() const{ return d_func()->tearoff;}/*! When a menu is torn off a second menu is shown to display the menu contents in a new window. When the menu is in this mode and the menu is visible returns true; otherwise false. \sa hideTearOffMenu() isTearOffEnabled()*/bool QMenu::isTearOffMenuVisible() const{ if (d_func()->tornPopup) return d_func()->tornPopup->isVisible(); return false;}/*! This function will forcibly hide the torn off menu making it disappear from the users desktop. \sa isTearOffMenuVisible() isTearOffEnabled()*/void QMenu::hideTearOffMenu(){ if (d_func()->tornPopup) d_func()->tornPopup->close();}/*! Sets the currently highlighted action to \a act.*/void QMenu::setActiveAction(QAction *act){ Q_D(QMenu); d->setCurrentAction(act, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -