📄 qmenu.cpp
字号:
destroyed (as with any other QObject). \sa title*/QMenu::QMenu(const QString &title, QWidget *parent) : QWidget(*new QMenuPrivate, parent, Qt::Popup){ Q_D(QMenu); d->init(); d->menuAction->setText(title);}/*! \internal */QMenu::QMenu(QMenuPrivate &dd, QWidget *parent) : QWidget(dd, parent, Qt::Popup){ Q_D(QMenu); d->init();}/*! 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(bool)), 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(bool)), 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); if (d->scroll) d->scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollCenter);}/*! Returns the currently highlighted action, or 0 if no action is currently highlighted.*/QAction *QMenu::activeAction() const{ return d_func()->currentAction;}/*! \since 4.2 Returns true if there are no actions inserted into the menu, false otherwise. \sa QWidget::actions()*/bool QMenu::isEmpty() const{ return actions().isEmpty();}/*! Removes all the menu's actions. Actions owned by the menu and not shown in any other widget are deleted. \sa removeAction()*/void QMenu::clear(){ QList<QAction*> acts = actions(); for(int i = 0; i < acts.size(); i++) { removeAction(acts[i]); if (acts[i]->parent() == this && acts[i]->d_func()->widgets.isEmpty()) delete acts[i]; }}/*! If a menu does not fit on the screen it lays itself out so that it does fit. It is style dependent what layout means (for example, on Windows it will use multiple columns). This functions returns the number of columns necessary.*/int QMenu::columnCount() const{ return d_func()->ncols;}/*! Returns the item at \a pt; returns 0 if there is no item there.*/QAction *QMenu::actionAt(const QPoint &pt) const{ if (QAction *ret = d_func()->actionAt(pt)) return ret; return 0;}/*! Returns the geometry of action \a act.*/QRect QMenu::actionGeometry(QAction *act) const{ return d_func()->actionRect(act);}/*! \reimp*/QSize QMenu::sizeHint() const{ Q_D(const QMenu); ensurePolished(); QMap<QAction*, QRect> actionRects; QList<QAction*> actionList; d->calcActionRects(actionRects, actionList); QSize s; QStyleOption opt(0); opt.rect = rect(); opt.palette = palette(); opt.state = QStyle::State_None; for (QMap<QAction*, QRect>::const_iterator i = actionRects.constBegin(); i != actionRects.constEnd(); ++i) { if (i.value().bottom() > s.height()) s.setHeight(i.value().y()+i.value().height()); if (i.value().right() > s.width()) s.setWidth(i.value().right()); } if (d->tearoff) s.rheight() += style()->pixelMetric(QStyle::PM_MenuTearoffHeight, &opt, this); if (const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, &opt, this)) { s.rwidth() += fw*2; s.rheight() += fw*2; } s.rwidth() += 2 * style()->pixelMetric(QStyle::PM_MenuHMargin, &opt, this); s.rheight() += 2 * style()->pixelMetric(QStyle::PM_MenuVMargin, &opt, this); s += QSize(d->leftmargin + d->rightmargin, d->topmargin + d->bottommargin); return style()->sizeFromContents(QStyle::CT_Menu, &opt, s.expandedTo(QApplication::globalStrut()), this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -