94 int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr, q);
95 QRect result = q->rect();
96 result.adjust(hmargin, 0, -hmargin, 0);
99 if (q->isRightToLeft())
100 result.setLeft(result.left() +
extension->sizeHint().width());
102 result.setWidth(result.width() -
extension->sizeHint().width());
105 if (leftWidget && leftWidget->isVisible()) {
106 QSize sz = leftWidget->sizeHint();
107 if (q->isRightToLeft())
108 result.setRight(result.right() - sz.width());
110 result.setLeft(result.left() + sz.width());
113 if (rightWidget && rightWidget->isVisible()) {
114 QSize sz = rightWidget->sizeHint();
115 if (q->isRightToLeft())
116 result.setLeft(result.left() + sz.width());
118 result.setRight(result.right() - sz.width());
134 int q_width = q->width()-(q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr, q)*2);
136 if (leftWidget || rightWidget) {
137 int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin,
nullptr, q)
138 + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr, q);
139 int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarHMargin,
nullptr, q)
140 + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr, q);
141 if (leftWidget && leftWidget->isVisible()) {
142 QSize sz = leftWidget->sizeHint();
143 q_width -= sz.width();
144 q_start = sz.width();
145 QPoint pos(hmargin, (q->height() - leftWidget->height()) / 2);
146 QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
147 leftWidget->setGeometry(vRect);
149 if (rightWidget && rightWidget->isVisible()) {
150 QSize sz = rightWidget->sizeHint();
151 q_width -= sz.width();
152 QPoint pos(q->width() - sz.width() - hmargin, vmargin);
153 QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
154 rightWidget->setGeometry(vRect);
159 if (q->isNativeMenuBar()) {
165 currentAction =
nullptr;
166#ifndef QT_NO_SHORTCUT
168 for(
int j = 0; j < shortcutIndexMap.size(); ++j)
169 q->releaseShortcut(shortcutIndexMap.value(j));
170 shortcutIndexMap.clear();
171 const int actionsCount = actions.size();
172 shortcutIndexMap.reserve(actionsCount);
173 for (
int i = 0; i < actionsCount; i++)
174 shortcutIndexMap.append(q->grabShortcut(QKeySequence::mnemonic(actions.at(i)->text())));
179 hiddenActions.clear();
181 QRect menuRect =
this->menuRect(
false);
184 bool hasHiddenActions =
false;
185 for (
int i = 0; i < actions.size(); ++i) {
186 const QRect &rect = actionRects.at(i);
187 if (rect.isValid() && !menuRect.contains(rect)) {
188 hasHiddenActions =
true;
194 if (hasHiddenActions) {
195 menuRect =
this->menuRect(
true);
196 for (
int i = 0; i < actions.size(); ++i) {
197 const QRect &rect = actionRects.at(i);
198 if (rect.isValid() && !menuRect.contains(rect)) {
199 hiddenActions.append(actions.at(i));
204 if (hiddenActions.size() > 0) {
205 QMenu *pop = extension->menu();
211 pop->addActions(hiddenActions);
213 int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin,
nullptr, q);
214 int x = q->isRightToLeft()
215 ? menuRect.left() - extension->sizeHint().width() + 1
217 extension->setGeometry(x, vmargin,
extension->sizeHint().width(), menuRect.height() - vmargin*2);
252 if (b && !q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation,
nullptr, q)) {
258 QWidget *fw = QApplication::focusWidget();
259 if (fw && fw != q && fw->window() != QApplication::activePopupWidget())
260 keyboardFocusWidget = fw;
262 q->setFocus(Qt::MenuBarFocusReason);
266 if (keyboardFocusWidget) {
267 if (QApplication::focusWidget() == q)
268 keyboardFocusWidget->setFocus(Qt::MenuBarFocusReason);
269 keyboardFocusWidget =
nullptr;
278 if (!action || !action->menu() || closePopupMode)
281 if (action->isEnabled() && action->menu()->isEnabled()) {
283 activeMenu = action->menu();
284 auto *activeMenuPriv = activeMenu->d_func();
285 activeMenuPriv->causedPopup.widget = q;
286 activeMenuPriv->causedPopup.action = action;
288 QRect adjustedActionRect = actionRect(action);
289 QPoint popupPos = adjustedActionRect.bottomLeft() + QPoint(0, 1);
292 QScreen *menubarScreen = q->window()->windowHandle()->screen();
293 QPoint screenTestPos = q->mapToGlobal(popupPos + QPoint(adjustedActionRect.width() / 2, 0));
294 QPointer<QScreen> popupScreen = menubarScreen->virtualSiblingAt(screenTestPos);
296 popupScreen = menubarScreen;
297 std::swap(popupScreen, activeMenuPriv->popupScreen);
298 const QSize popup_size = activeMenu->sizeHint();
299 std::swap(popupScreen, activeMenuPriv->popupScreen);
304 QPoint pos(q->mapToGlobal(popupPos).x(), screenTestPos.y());
306 QRect screenRect = popupScreen->geometry();
307 pos = QPoint(qMax(pos.x(), screenRect.x()), qMax(pos.y(), screenRect.y()));
308 const bool fitUp = (pos.y() - popup_size.height() >= screenRect.top());
309 const bool fitDown = (pos.y() + popup_size.height() <= screenRect.bottom());
310 const bool rtl = q->isRightToLeft();
311 const int actionWidth = adjustedActionRect.width();
313 if (!fitUp && !fitDown) {
314 bool shouldShiftToRight = !rtl;
315 if (rtl && popup_size.width() > pos.x())
316 shouldShiftToRight =
true;
317 else if (actionWidth + popup_size.width() + pos.x() > screenRect.right())
318 shouldShiftToRight =
false;
320 if (shouldShiftToRight) {
321 pos.rx() += actionWidth + (rtl ? popup_size.width() : 0);
325 pos.rx() -= popup_size.width();
328 pos.rx() += actionWidth;
331 if (!defaultPopDown || (fitUp && !fitDown))
332 pos.setY(qMax(screenRect.y(), q->mapToGlobal(QPoint(0, adjustedActionRect.top()-popup_size.height())).y()));
333 QMenuPrivate::get(activeMenu)->topData()->initialScreen = popupScreen;
334 activeMenu->popup(pos);
336 activeMenu->d_func()->setFirstActionActive();
338 q->update(actionRect(action));
343 if (currentAction == action && popup == popupState)
346 autoReleaseTimer.stop();
348 doChildEffects = (popup && !activeMenu);
351 if (QMenu *menu = activeMenu) {
352 activeMenu =
nullptr;
354 fw = q->window()->focusWidget();
355 q->setFocus(Qt::NoFocusReason);
361 q->update(actionRect(currentAction));
364#if QT_CONFIG(statustip)
365 QAction *previousAction = currentAction;
367 currentAction = action;
368 if (action && action->isEnabled()) {
369 activateAction(action, QAction::Hover);
372 q->update(actionRect(action));
373#if QT_CONFIG(statustip)
374 }
else if (previousAction) {
376 QStatusTipEvent tip(empty);
377 QCoreApplication::sendEvent(q, &tip);
381 fw->setFocus(Qt::NoFocusReason);
392 actionRects.resize(actions.size());
393 actionRects.fill(QRect());
395 const QStyle *style = q->style();
397 const int itemSpacing = style->pixelMetric(QStyle::PM_MenuBarItemSpacing,
nullptr, q);
398 int max_item_height = 0, separator = -1, separator_start = 0, separator_len = 0;
401 const QFontMetrics fm = q->fontMetrics();
402 const int hmargin = style->pixelMetric(QStyle::PM_MenuBarHMargin,
nullptr, q),
403 vmargin = style->pixelMetric(QStyle::PM_MenuBarVMargin,
nullptr, q),
404 icone = style->pixelMetric(QStyle::PM_SmallIconSize,
nullptr, q);
405 for(
int i = 0; i < actions.size(); i++) {
406 QAction *action = actions.at(i);
407 if (!action->isVisible())
413 if (action->isSeparator()) {
414 if (style->styleHint(QStyle::SH_DrawMenuBarSeparator,
nullptr, q))
418 const QString s = action->text();
419 QIcon is = action->icon();
422 sz = sz.expandedTo(QSize(icone, icone));
423 else if (!s.isEmpty())
424 sz = fm.size(Qt::TextShowMnemonic, s);
428 QStyleOptionMenuItem opt;
429 q->initStyleOption(&opt, action);
430 sz = q->style()->sizeFromContents(QStyle::CT_MenuBarItem, &opt, sz, q);
434 int iWidth = sz.width() + itemSpacing;
436 separator_start += iWidth;
438 separator_len += iWidth;
441 max_item_height = qMax(max_item_height, sz.height());
443 actionRects[i] = QRect(0, 0, sz.width(), sz.height());
448 const int fw = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr, q);
449 int x = fw + ((start == -1) ? hmargin : start) + itemSpacing;
450 int y = fw + vmargin;
451 for(
int i = 0; i < actions.size(); i++) {
452 QRect &rect = actionRects[i];
457 rect.setHeight(max_item_height);
460 if (separator != -1 && i >= separator) {
461 int left = (max_width - separator_len - hmargin - itemSpacing) + (x - separator_start - hmargin);
462 if (left < separator_start) {
463 separator_start = x = hmargin;
464 y += max_item_height;
473 x += rect.width() + itemSpacing;
476 rect = QStyle::visualRect(q->layoutDirection(), q->rect(), rect);
507 if (QAction *action = qobject_cast<QAction *>(q->sender())) {
508 emit q->hovered(action);
509#if QT_CONFIG(accessibility)
510 if (QAccessible::isActive()) {
511 int actionIndex = actions.indexOf(action);
512 QAccessibleEvent focusEvent(q, QAccessible::Focus);
513 focusEvent.setChild(actionIndex);
514 QAccessible::updateAccessibility(&focusEvent);
527void QMenuBar::initStyleOption(QStyleOptionMenuItem *option,
const QAction *action)
const
529 if (!option || !action)
532 option->palette = palette();
533 option->state = QStyle::State_None;
534 if (isEnabled() && action->isEnabled())
535 option->state |= QStyle::State_Enabled;
537 option->palette.setCurrentColorGroup(QPalette::Disabled);
538 option->fontMetrics = fontMetrics();
539 if (d->currentAction && d->currentAction == action) {
540 option->state |= QStyle::State_Selected;
541 if (d->popupState && !d->closePopupMode)
542 option->state |= QStyle::State_Sunken;
544 if (hasFocus() || d->currentAction)
545 option->state |= QStyle::State_HasFocus;
546 option->menuRect = rect();
547 option->menuItemType = QStyleOptionMenuItem::Normal;
548 option->checkType = QStyleOptionMenuItem::NotCheckable;
549 option->text = action->text();
550 option->icon = action->icon();
673 q->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
674 q->setAttribute(Qt::WA_CustomWhatsThis);
676 if (!QCoreApplication::testAttribute(Qt::AA_DontUseNativeMenuBar))
677 platformMenuBar = QGuiApplicationPrivate::platformTheme()->createPlatformMenuBar();
681 q->setBackgroundRole(QPalette::Button);
683 q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking,
nullptr, q));
685 extension =
new QMenuBarExtension(q);
686 extension->setFocusPolicy(Qt::NoFocus);
893void QMenuBar::paintEvent(QPaintEvent *e)
897 QRegion emptyArea(rect());
900 for (
int i = 0; i < d->actions.size(); ++i) {
901 QAction *action = d->actions.at(i);
902 QRect adjustedActionRect = d->actionRect(action);
903 if (adjustedActionRect.isEmpty() || !d->isVisible(action))
905 if (!e->rect().intersects(adjustedActionRect))
908 emptyArea -= adjustedActionRect;
909 QStyleOptionMenuItem opt;
910 initStyleOption(&opt, action);
911 opt.rect = adjustedActionRect;
912 p.setClipRect(adjustedActionRect);
913 style()->drawControl(QStyle::CE_MenuBarItem, &opt, &p,
this);
916 if (
int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr,
this)) {
918 borderReg += QRect(0, 0, fw, height());
919 borderReg += QRect(width()-fw, 0, fw, height());
920 borderReg += QRect(0, 0, width(), fw);
921 borderReg += QRect(0, height()-fw, width(), fw);
922 p.setClipRegion(borderReg);
923 emptyArea -= borderReg;
924 QStyleOptionFrame frame;
926 frame.palette = palette();
927 frame.state = QStyle::State_None;
928 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, &frame,
this);
929 frame.midLineWidth = 0;
930 style()->drawPrimitive(QStyle::PE_PanelMenuBar, &frame, &p,
this);
932 p.setClipRegion(emptyArea);
933 QStyleOptionMenuItem menuOpt;
934 menuOpt.palette = palette();
935 menuOpt.state = QStyle::State_None;
936 menuOpt.menuItemType = QStyleOptionMenuItem::EmptyArea;
937 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
938 menuOpt.rect = rect();
939 menuOpt.menuRect = rect();
940 style()->drawControl(QStyle::CE_MenuBarEmptyArea, &menuOpt, &p,
this);
959void QMenuBar::mousePressEvent(QMouseEvent *e)
962 if (e->button() != Qt::LeftButton)
967 QAction *action = d->actionAt(e->position().toPoint());
968 if (!action || !d->isVisible(action) || !action->isEnabled()) {
969 d->setCurrentAction(
nullptr);
970#if QT_CONFIG(whatsthis)
971 if (QWhatsThis::inWhatsThisMode())
972 QWhatsThis::showText(e->globalPosition().toPoint(), d->whatsThis,
this);
977 if (d->currentAction == action && d->popupState) {
978 if (QMenu *menu = d->activeMenu) {
979 d->activeMenu =
nullptr;
980 menu->setAttribute(Qt::WA_NoMouseReplay);
984 d->setCurrentAction(action,
true);
991void QMenuBar::mouseReleaseEvent(QMouseEvent *e)
994 if (e->button() != Qt::LeftButton || !d->mouseDown)
997 d->mouseDown =
false;
998 QAction *action = d->actionAt(e->position().toPoint());
1001 if (!d->isVisible(action))
1003 if ((d->closePopupMode && action == d->currentAction) || !action || !action->menu()) {
1006 d->setCurrentAction(action,
false);
1008 d->activateAction(action, QAction::Trigger);
1010 d->closePopupMode = 0;
1016void QMenuBar::keyPressEvent(QKeyEvent *e)
1019 d->updateGeometries();
1021 if (isRightToLeft()) {
1022 if (key == Qt::Key_Left)
1023 key = Qt::Key_Right;
1024 else if (key == Qt::Key_Right)
1027 if (key == Qt::Key_Tab)
1028 key = Qt::Key_Right;
1029 else if (key == Qt::Key_Backtab)
1032 bool key_consumed =
false;
1038 case Qt::Key_Return: {
1039 if (!style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation,
nullptr,
this) || !d->currentAction)
1041 if (d->currentAction->menu()) {
1042 d->popupAction(d->currentAction,
true);
1043 }
else if (key == Qt::Key_Enter || key == Qt::Key_Return || key == Qt::Key_Space) {
1044 d->activateAction(d->currentAction, QAction::Trigger);
1045 d->setCurrentAction(d->currentAction,
false);
1046 d->setKeyboardMode(
false);
1048 key_consumed =
true;
1052 case Qt::Key_Left: {
1053 if (d->currentAction) {
1054 int index = d->actions.indexOf(d->currentAction);
1055 if (QAction *nextAction = d->getNextAction(index, key == Qt::Key_Left ? -1 : +1)) {
1056 d->setCurrentAction(nextAction, d->popupState,
true);
1057 key_consumed =
true;
1063 key_consumed =
false;
1066#ifndef QT_NO_SHORTCUT
1067 if (!key_consumed && e->matches(QKeySequence::Cancel)) {
1068 d->setCurrentAction(
nullptr);
1069 d->setKeyboardMode(
false);
1070 key_consumed =
true;
1074 if (!key_consumed &&
1076 (e->modifiers()&(Qt::MetaModifier|Qt::AltModifier))) && e->text().size()==1 && !d->popupState) {
1078 QAction *first =
nullptr, *currentSelected =
nullptr, *firstAfterCurrent =
nullptr;
1080 const QChar c = e->text().at(0).toUpper();
1081 for(
int i = 0; i < d->actions.size(); ++i) {
1082 if (d->actionRects.at(i).isNull())
1084 QAction *act = d->actions.at(i);
1085 QString s = act->text();
1087 qsizetype ampersand = s.indexOf(u'&');
1088 if (ampersand >= 0) {
1089 if (s[ampersand+1].toUpper() == c) {
1093 if (act == d->currentAction)
1094 currentSelected = act;
1095 else if (!firstAfterCurrent && currentSelected)
1096 firstAfterCurrent = act;
1102 QAction *next_action =
nullptr;
1103 if (clashCount >= 1) {
1104 if (clashCount == 1 || !d->currentAction || (currentSelected && !firstAfterCurrent))
1105 next_action = first;
1107 next_action = firstAfterCurrent;
1110 key_consumed =
true;
1111 d->setCurrentAction(next_action,
true,
true);
1123void QMenuBar::mouseMoveEvent(QMouseEvent *e)
1126 if (!(e->buttons() & Qt::LeftButton)) {
1127 d->mouseDown =
false;
1131 if (e->source() != Qt::MouseEventNotSynthesized)
1135 bool popupState = d->popupState || d->mouseDown;
1136 QAction *action = d->actionAt(e->position().toPoint());
1137 if ((action && d->isVisible(action)) || !popupState)
1138 d->setCurrentAction(action, popupState);
1193void QMenuBar::actionEvent(QActionEvent *e)
1196 d->itemsDirty =
true;
1198 if (d->platformMenuBar) {
1199 QPlatformMenuBar *nativeMenuBar = d->platformMenuBar;
1203 auto action =
static_cast<QAction *>(e->action());
1204 if (e->type() == QEvent::ActionAdded) {
1205 QPlatformMenu *menu = d->getPlatformMenu(action);
1207 d->copyActionToPlatformMenu(action, menu);
1209 QPlatformMenu *beforeMenu = d->findInsertionPlatformMenu(action);
1210 d->platformMenuBar->insertMenu(menu, beforeMenu);
1212 }
else if (e->type() == QEvent::ActionRemoved) {
1213 QPlatformMenu *menu = d->getPlatformMenu(action);
1215 d->platformMenuBar->removeMenu(menu);
1216 }
else if (e->type() == QEvent::ActionChanged) {
1217 QPlatformMenu *cur = d->platformMenuBar->menuForTag(
reinterpret_cast<quintptr>(e->action()));
1218 QPlatformMenu *menu = d->getPlatformMenu(action);
1224 d->platformMenuBar->removeMenu(cur);
1226 d->copyActionToPlatformMenu(action, menu);
1228 QPlatformMenu *beforeMenu = d->findInsertionPlatformMenu(action);
1229 d->platformMenuBar->insertMenu(menu, beforeMenu);
1232 d->copyActionToPlatformMenu(action, menu);
1233 d->platformMenuBar->syncMenu(menu);
1238 if (e->type() == QEvent::ActionAdded) {
1239 connect(e->action(), SIGNAL(triggered()),
this, SLOT(_q_actionTriggered()));
1240 connect(e->action(), SIGNAL(hovered()),
this, SLOT(_q_actionHovered()));
1241 }
else if (e->type() == QEvent::ActionRemoved) {
1242 e->action()->disconnect(
this);
1246 if (isVisible() || isNativeMenuBar())
1247 d->updateGeometries();
1291 QWidget *newParent = q->parentWidget();
1296 QWidget *newWindow = newParent ? newParent->window() :
nullptr;
1298 QList<QPointer<QWidget>> newParents;
1305 const auto copy = oldParents;
1306 for (
const QPointer<QWidget> &w : copy) {
1308 if (newParent == w) {
1309 newParents.append(w);
1310 if (newParent != newWindow)
1311 newParent = newParent->parentWidget();
1313 w->removeEventFilter(q);
1319 while (newParent && newParent != newWindow) {
1321 newParents.append(newParent);
1322 newParent->installEventFilter(q);
1323 newParent = newParent->parentWidget();
1326 if (newParent && newWindow) {
1328 newParents.append(newParent);
1329 newParent->installEventFilter(q);
1331 oldParents = newParents;
1338 newWindow->createWinId();
1349void QMenuBar::changeEvent(QEvent *e)
1352 if (e->type() == QEvent::StyleChange) {
1353 d->itemsDirty =
true;
1354 setMouseTracking(style()->styleHint(QStyle::SH_MenuBar_MouseTracking,
nullptr,
this));
1356 resize(parentWidget()->width(), heightForWidth(parentWidget()->width()));
1357 d->updateGeometries();
1358 }
else if (e->type() == QEvent::ParentChange) {
1359 d->handleReparent();
1360 }
else if (e->type() == QEvent::FontChange
1361 || e->type() == QEvent::ApplicationFontChange) {
1362 d->itemsDirty =
true;
1363 d->updateGeometries();
1366 QWidget::changeEvent(e);
1372bool QMenuBar::event(QEvent *e)
1375 switch (e->type()) {
1376 case QEvent::KeyPress: {
1377 QKeyEvent *ke =
static_cast<QKeyEvent *>(e);
1379 if (!d->keyboardState) {
1380 d->setCurrentAction(0);
1384 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
1390#ifndef QT_NO_SHORTCUT
1391 case QEvent::Shortcut: {
1392 QShortcutEvent *se =
static_cast<QShortcutEvent *>(e);
1393 int shortcutId = se->shortcutId();
1394 for(
int j = 0; j < d->shortcutIndexMap.size(); ++j) {
1395 if (shortcutId == d->shortcutIndexMap.value(j))
1396 d->_q_internalShortcutActivated(j);
1401 d->_q_updateLayout();
1403#ifndef QT_NO_SHORTCUT
1404 case QEvent::ShortcutOverride: {
1405 QKeyEvent *kev =
static_cast<QKeyEvent *>(e);
1407 if (kev->matches(QKeySequence::Cancel) && d->currentAction) {
1414#if QT_CONFIG(whatsthis)
1415 case QEvent::QueryWhatsThis:
1416 e->setAccepted(d->whatsThis.size());
1417 if (QAction *action = d->actionAt(
static_cast<QHelpEvent*>(e)->pos())) {
1418 if (action->whatsThis().size() || action->menu())
1423 case QEvent::LayoutDirectionChange:
1424 d->_q_updateLayout();
1429 return QWidget::event(e);
1435bool QMenuBar::eventFilter(QObject *object, QEvent *event)
1438 if (object && (event->type() == QEvent::ParentChange))
1439 d->handleReparent();
1441 if (object == d->leftWidget || object == d->rightWidget) {
1442 switch (event->type()) {
1443 case QEvent::ShowToParent:
1444 case QEvent::HideToParent:
1445 d->_q_updateLayout();
1452 if (isNativeMenuBar() && event->type() == QEvent::ShowToParent) {
1457 QWidget *widget = qobject_cast<QWidget *>(object);
1458 QWindow *handle = widget ? widget->windowHandle() :
nullptr;
1459 if (handle !=
nullptr)
1460 d->platformMenuBar->handleReparent(handle);
1463 if (style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation,
nullptr,
this)) {
1464 if (d->altPressed) {
1465 switch (event->type()) {
1466 case QEvent::KeyPress:
1467 case QEvent::KeyRelease:
1469 QKeyEvent *kev =
static_cast<QKeyEvent*>(event);
1470 if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) {
1471 if (event->type() == QEvent::KeyPress)
1473 d->setKeyboardMode(!d->keyboardState);
1477 case QEvent::MouseButtonPress:
1478 case QEvent::MouseButtonRelease:
1479 case QEvent::MouseMove:
1480 case QEvent::FocusIn:
1481 case QEvent::FocusOut:
1482 case QEvent::ActivationChange:
1483 case QEvent::Shortcut:
1484 d->altPressed =
false;
1485 qApp->removeEventFilter(
this);
1490 }
else if (isVisible()) {
1491 if (event->type() == QEvent::ShortcutOverride) {
1492 QKeyEvent *kev =
static_cast<QKeyEvent*>(event);
1493 if ((kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta)
1494 && kev->modifiers() == Qt::AltModifier) {
1495 d->altPressed =
true;
1496 qApp->installEventFilter(
this);
1531QSize QMenuBar::minimumSizeHint()
const
1533 Q_D(
const QMenuBar);
1534 const bool as_gui_menubar = !isNativeMenuBar();
1538 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1539 const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin,
nullptr,
this);
1540 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin,
nullptr,
this);
1541 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr,
this);
1542 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar,
nullptr,
this);
1543 if (as_gui_menubar) {
1544 int w = parentWidget() ? parentWidget()->width() : QGuiApplication::primaryScreen()->virtualGeometry().width();
1545 d->calcActionRects(w - (2 * fw), 0);
1546 for (
int i = 0; ret.isNull() && i < d->actions.size(); ++i)
1547 ret = d->actionRects.at(i).size();
1548 if (!d->extension->isHidden())
1549 ret += QSize(d->extension->sizeHint().width(), 0);
1550 ret += QSize(2*fw + hmargin, 2*fw + vmargin);
1552 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1553 if (d->leftWidget) {
1554 QSize sz = d->leftWidget->minimumSizeHint();
1555 ret.setWidth(ret.width() + sz.width());
1556 if (sz.height() + margin > ret.height())
1557 ret.setHeight(sz.height() + margin);
1559 if (d->rightWidget) {
1560 QSize sz = d->rightWidget->minimumSizeHint();
1561 ret.setWidth(ret.width() + sz.width());
1562 if (sz.height() + margin > ret.height())
1563 ret.setHeight(sz.height() + margin);
1565 if (as_gui_menubar) {
1566 QStyleOptionMenuItem opt;
1568 opt.menuRect = rect();
1569 opt.state = QStyle::State_None;
1570 opt.menuItemType = QStyleOptionMenuItem::Normal;
1571 opt.checkType = QStyleOptionMenuItem::NotCheckable;
1572 opt.palette = palette();
1573 return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, ret,
this);
1581QSize QMenuBar::sizeHint()
const
1583 Q_D(
const QMenuBar);
1584 const bool as_gui_menubar = !isNativeMenuBar();
1588 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1589 const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin,
nullptr,
this);
1590 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin,
nullptr,
this);
1591 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr,
this);
1592 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar,
nullptr,
this);
1593 if (as_gui_menubar) {
1594 const int w = parentWidget() ? parentWidget()->width() : QGuiApplication::primaryScreen()->virtualGeometry().width();
1595 d->calcActionRects(w - (2 * fw), 0);
1596 for (
int i = 0; i < d->actionRects.size(); ++i) {
1597 const QRect &actionRect = d->actionRects.at(i);
1598 ret = ret.expandedTo(QSize(actionRect.x() + actionRect.width(), actionRect.y() + actionRect.height()));
1602 ret += QSize(fw + hmargin, fw + vmargin);
1604 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1605 if (d->leftWidget) {
1606 QSize sz = d->leftWidget->sizeHint();
1607 sz.rheight() += margin;
1608 ret = ret.expandedTo(sz);
1610 if (d->rightWidget) {
1611 QSize sz = d->rightWidget->sizeHint();
1612 ret.setWidth(ret.width() + sz.width());
1613 if (sz.height() + margin > ret.height())
1614 ret.setHeight(sz.height() + margin);
1616 if (as_gui_menubar) {
1617 QStyleOptionMenuItem opt;
1619 opt.menuRect = rect();
1620 opt.state = QStyle::State_None;
1621 opt.menuItemType = QStyleOptionMenuItem::Normal;
1622 opt.checkType = QStyleOptionMenuItem::NotCheckable;
1623 opt.palette = palette();
1624 return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, ret,
this);
1632int QMenuBar::heightForWidth(
int)
const
1634 Q_D(
const QMenuBar);
1635 const bool as_gui_menubar = !isNativeMenuBar();
1637 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1639 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin,
nullptr,
this);
1640 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth,
nullptr,
this);
1641 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar,
nullptr,
this);
1642 if (as_gui_menubar) {
1643 for (
int i = 0; i < d->actionRects.size(); ++i)
1644 height = qMax(height, d->actionRects.at(i).height());
1646 height += spaceBelowMenuBar;
1648 height += 2*vmargin;
1650 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1652 height = qMax(d->leftWidget->sizeHint().height() + margin, height);
1654 height = qMax(d->rightWidget->sizeHint().height() + margin, height);
1655 if (as_gui_menubar) {
1656 QStyleOptionMenuItem opt;
1658 opt.menuRect = rect();
1659 opt.state = QStyle::State_None;
1660 opt.menuItemType = QStyleOptionMenuItem::Normal;
1661 opt.checkType = QStyleOptionMenuItem::NotCheckable;
1662 return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, QSize(0, height),
this).height();
1673 QAction *act = actions.at(id);
1674 if (act && act->menu()) {
1675 if (QPlatformMenu *platformMenu = act->menu()->platformMenu()) {
1676 platformMenu->showPopup(q->windowHandle(), actionRects.at(id),
nullptr);
1681 keyboardFocusWidget = QApplication::focusWidget();
1683 if (act && !act->menu()) {
1684 activateAction(act, QAction::Trigger);
1686 autoReleaseTimer.start(100, q);
1687 }
else if (act && q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation,
nullptr, q)) {