Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qmenubar.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qmenubar.h>
5
6#include <qstyle.h>
7#include <qlayout.h>
8#include <qapplication.h>
9#if QT_CONFIG(accessibility)
10# include <qaccessible.h>
11#endif
12#include <qpainter.h>
13#include <qstylepainter.h>
14#include <qevent.h>
15#if QT_CONFIG(mainwindow)
16#include <qmainwindow.h>
17#endif
18#if QT_CONFIG(toolbar)
19#include <qtoolbar.h>
20#endif
21#if QT_CONFIG(toolbutton)
22#include <qtoolbutton.h>
23#endif
24#if QT_CONFIG(whatsthis)
25#include <qwhatsthis.h>
26#endif
27#include <qpa/qplatformtheme.h>
28#include "private/qguiapplication_p.h"
29#include "qpa/qplatformintegration.h"
30
31#include "qmenu_p.h"
32#include "qmenubar_p.h"
33#include <private/qscreen_p.h>
34#include "qdebug.h"
35
37
38using namespace Qt::StringLiterals;
39
41{
42public:
44
45 QSize sizeHint() const override;
46 void paintEvent(QPaintEvent *) override;
47};
48
51{
52 setObjectName("qt_menubar_ext_button"_L1);
53 setAutoRaise(true);
54#if QT_CONFIG(menu)
55 setPopupMode(QToolButton::InstantPopup);
56#endif
58}
59
61{
62 QStylePainter p(this);
65 // We do not need to draw both extension arrows
67 p.drawComplexControl(QStyle::CC_ToolButton, opt);
68}
69
70
72{
74 return QSize(ext, ext);
75}
76
77
82{
83 for(int i = 0; i < actions.size(); ++i) {
85 return actions.at(i);
86 }
87 return nullptr;
88}
89
90QRect QMenuBarPrivate::menuRect(bool extVisible) const
91{
92 Q_Q(const QMenuBar);
93
94 int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, nullptr, q);
95 QRect result = q->rect();
96 result.adjust(hmargin, 0, -hmargin, 0);
97
98 if (extVisible) {
99 if (q->isRightToLeft())
100 result.setLeft(result.left() + extension->sizeHint().width());
101 else
102 result.setWidth(result.width() - extension->sizeHint().width());
103 }
104
105 if (leftWidget && leftWidget->isVisible()) {
106 QSize sz = leftWidget->sizeHint();
107 if (q->isRightToLeft())
108 result.setRight(result.right() - sz.width());
109 else
110 result.setLeft(result.left() + sz.width());
111 }
112
114 QSize sz = rightWidget->sizeHint();
115 if (q->isRightToLeft())
116 result.setLeft(result.left() + sz.width());
117 else
118 result.setRight(result.right() - sz.width());
119 }
120
121 return result;
122}
123
125{
126 return !hiddenActions.contains(action);
127}
128
130{
131 Q_Q(QMenuBar);
132 if (!itemsDirty)
133 return;
134 int q_width = q->width()-(q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, nullptr, q)*2);
135 int q_start = -1;
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);
148 }
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);
155 }
156 }
157
158#ifdef Q_OS_MAC
159 if (q->isNativeMenuBar()) {//nothing to see here folks, move along..
160 itemsDirty = false;
161 return;
162 }
163#endif
164 calcActionRects(q_width, q_start);
165 currentAction = nullptr;
166#ifndef QT_NO_SHORTCUT
167 if (itemsDirty) {
168 for(int j = 0; j < shortcutIndexMap.size(); ++j)
169 q->releaseShortcut(shortcutIndexMap.value(j));
171 const int actionsCount = actions.size();
172 shortcutIndexMap.reserve(actionsCount);
173 for (int i = 0; i < actionsCount; i++)
175 }
176#endif
177 itemsDirty = false;
178
180 //this is the menu rectangle without any extension
181 QRect menuRect = this->menuRect(false);
182
183 //we try to see if the actions will fit there
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;
189 break;
190 }
191 }
192
193 //...and if not, determine the ones that fit on the menu with the extension visible
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)) {
200 }
201 }
202 }
203
204 if (hiddenActions.size() > 0) {
205 QMenu *pop = extension->menu();
206 if (!pop) {
207 pop = new QMenu(q);
208 extension->setMenu(pop);
209 }
210 pop->clear();
212
213 int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, nullptr, q);
214 int x = q->isRightToLeft()
215 ? menuRect.left() - extension->sizeHint().width() + 1
216 : menuRect.right();
217 extension->setGeometry(x, vmargin, extension->sizeHint().width(), menuRect.height() - vmargin*2);
218 extension->show();
219 } else {
220 extension->hide();
221 }
222 q->updateGeometry();
223}
224
226{
227 const int index = actions.indexOf(act);
228
229 //makes sure the geometries are up-to-date
230 const_cast<QMenuBarPrivate*>(this)->updateGeometries();
231
232 if (index < 0 || index >= actionRects.size())
233 return QRect(); // that can happen in case of native menubar
234
235 return actionRects.at(index);
236}
237
239{
240 if (!currentAction) {
242 int index = 0;
243 while (index < actions.size() && actionRects.at(index).isNull()) ++index;
244 if (index < actions.size())
246 }
247}
248
250{
251 Q_Q(QMenuBar);
252 if (b && !q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, nullptr, q)) {
253 setCurrentAction(nullptr);
254 return;
255 }
257 if (b) {
259 if (fw && fw != q && fw->window() != QApplication::activePopupWidget())
262 q->setFocus(Qt::MenuBarFocusReason);
263 } else {
264 if (!popupState)
265 setCurrentAction(nullptr);
269 keyboardFocusWidget = nullptr;
270 }
271 }
272 q->update();
273}
274
275void QMenuBarPrivate::popupAction(QAction *action, bool activateFirst)
276{
277 Q_Q(QMenuBar);
278 if (!action || !action->menu() || closePopupMode)
279 return;
280 popupState = true;
281 if (action->isEnabled() && action->menu()->isEnabled()) {
282 closePopupMode = 0;
283 activeMenu = action->menu();
284 activeMenu->d_func()->causedPopup.widget = q;
285 activeMenu->d_func()->causedPopup.action = action;
286
287 QRect adjustedActionRect = actionRect(action);
288 QPoint pos(q->mapToGlobal(QPoint(adjustedActionRect.left(), adjustedActionRect.bottom() + 1)));
289 QSize popup_size = activeMenu->sizeHint();
290 //we put the popup menu on the screen containing the bottom-center of the action rect
291 QScreen *menubarScreen = q->window()->windowHandle()->screen();
292 QScreen *popupScreen = menubarScreen->virtualSiblingAt(pos + QPoint(adjustedActionRect.width() / 2, 0));
293 if (!popupScreen)
294 popupScreen = menubarScreen;
295 QRect screenRect = popupScreen->geometry();
296 pos = QPoint(qMax(pos.x(), screenRect.x()), qMax(pos.y(), screenRect.y()));
297 const bool fitUp = (pos.y() - popup_size.height() >= screenRect.top());
298 const bool fitDown = (pos.y() + popup_size.height() <= screenRect.bottom());
299 const bool rtl = q->isRightToLeft();
300 const int actionWidth = adjustedActionRect.width();
301
302 if (!fitUp && !fitDown) { //we should shift the menu
303 bool shouldShiftToRight = !rtl;
304 if (rtl && popup_size.width() > pos.x())
305 shouldShiftToRight = true;
306 else if (actionWidth + popup_size.width() + pos.x() > screenRect.right())
307 shouldShiftToRight = false;
308
309 if (shouldShiftToRight) {
310 pos.rx() += actionWidth + (rtl ? popup_size.width() : 0);
311 } else {
312 //shift to left
313 if (!rtl)
314 pos.rx() -= popup_size.width();
315 }
316 } else if (rtl) {
317 pos.rx() += actionWidth;
318 }
319
320 if (!defaultPopDown || (fitUp && !fitDown))
321 pos.setY(qMax(screenRect.y(), q->mapToGlobal(QPoint(0, adjustedActionRect.top()-popup_size.height())).y()));
324 if (activateFirst)
325 activeMenu->d_func()->setFirstActionActive();
326 }
327 q->update(actionRect(action));
328}
329
330void QMenuBarPrivate::setCurrentAction(QAction *action, bool popup, bool activateFirst)
331{
332 if (currentAction == action && popup == popupState)
333 return;
334
336
337 doChildEffects = (popup && !activeMenu);
338 Q_Q(QMenuBar);
339 QWidget *fw = nullptr;
340 if (QMenu *menu = activeMenu) {
341 activeMenu = nullptr;
342 if (popup) {
343 fw = q->window()->focusWidget();
345 }
346 menu->hide();
347 }
348
349 if (currentAction)
350 q->update(actionRect(currentAction));
351
352 popupState = popup;
353#if QT_CONFIG(statustip)
354 QAction *previousAction = currentAction;
355#endif
356 currentAction = action;
357 if (action && action->isEnabled()) {
359 if (popup)
360 popupAction(action, activateFirst);
361 q->update(actionRect(action));
362#if QT_CONFIG(statustip)
363 } else if (previousAction) {
364 QString empty;
365 QStatusTipEvent tip(empty);
367#endif
368 }
369 if (fw)
371}
372
373void QMenuBarPrivate::calcActionRects(int max_width, int start) const
374{
375 Q_Q(const QMenuBar);
376
377 if (!itemsDirty)
378 return;
379
380 //let's reinitialize the buffer
383
384 const QStyle *style = q->style();
385
386 const int itemSpacing = style->pixelMetric(QStyle::PM_MenuBarItemSpacing, nullptr, q);
387 int max_item_height = 0, separator = -1, separator_start = 0, separator_len = 0;
388
389 //calculate size
390 const QFontMetrics fm = q->fontMetrics();
391 const int hmargin = style->pixelMetric(QStyle::PM_MenuBarHMargin, nullptr, q),
392 vmargin = style->pixelMetric(QStyle::PM_MenuBarVMargin, nullptr, q),
393 icone = style->pixelMetric(QStyle::PM_SmallIconSize, nullptr, q);
394 for(int i = 0; i < actions.size(); i++) {
395 QAction *action = actions.at(i);
396 if (!action->isVisible())
397 continue;
398
399 QSize sz;
400
401 //calc what I think the size is..
402 if (action->isSeparator()) {
403 if (style->styleHint(QStyle::SH_DrawMenuBarSeparator, nullptr, q))
404 separator = i;
405 continue; //we don't really position these!
406 } else {
407 const QString s = action->text();
408 QIcon is = action->icon();
409 // If an icon is set, only the icon is visible
410 if (!is.isNull())
411 sz = sz.expandedTo(QSize(icone, icone));
412 else if (!s.isEmpty())
413 sz = fm.size(Qt::TextShowMnemonic, s);
414 }
415
416 //let the style modify the above size..
418 q->initStyleOption(&opt, action);
419 sz = q->style()->sizeFromContents(QStyle::CT_MenuBarItem, &opt, sz, q);
420
421 if (!sz.isEmpty()) {
422 { //update the separator state
423 int iWidth = sz.width() + itemSpacing;
424 if (separator == -1)
425 separator_start += iWidth;
426 else
427 separator_len += iWidth;
428 }
429 //maximum height
430 max_item_height = qMax(max_item_height, sz.height());
431 //append
432 actionRects[i] = QRect(0, 0, sz.width(), sz.height());
433 }
434 }
435
436 //calculate position
437 const int fw = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, nullptr, q);
438 int x = fw + ((start == -1) ? hmargin : start) + itemSpacing;
439 int y = fw + vmargin;
440 for(int i = 0; i < actions.size(); i++) {
442 if (rect.isNull())
443 continue;
444
445 //resize
446 rect.setHeight(max_item_height);
447
448 //move
449 if (separator != -1 && i >= separator) { //after the separator
450 int left = (max_width - separator_len - hmargin - itemSpacing) + (x - separator_start - hmargin);
451 if (left < separator_start) { //wrap
452 separator_start = x = hmargin;
453 y += max_item_height;
454 }
455 rect.moveLeft(left);
456 } else {
457 rect.moveLeft(x);
458 }
459 rect.moveTop(y);
460
461 //keep moving along..
462 x += rect.width() + itemSpacing;
463
464 //make sure we follow the layout direction
465 rect = QStyle::visualRect(q->layoutDirection(), q->rect(), rect);
466 }
467}
468
470{
471 Q_Q(QMenuBar);
472 if (!action || !action->isEnabled())
473 return;
474 action->activate(action_e);
475 if (action_e == QAction::Hover)
476 action->showStatusText(q);
477
478// if (action_e == QAction::Trigger)
479// emit q->activated(action);
480// else if (action_e == QAction::Hover)
481// emit q->highlighted(action);
482}
483
484
486{
487 Q_Q(QMenuBar);
488 if (QAction *action = qobject_cast<QAction *>(q->sender())) {
489 emit q->triggered(action);
490 }
491}
492
494{
495 Q_Q(QMenuBar);
496 if (QAction *action = qobject_cast<QAction *>(q->sender())) {
497 emit q->hovered(action);
498#if QT_CONFIG(accessibility)
499 if (QAccessible::isActive()) {
500 int actionIndex = actions.indexOf(action);
501 QAccessibleEvent focusEvent(q, QAccessible::Focus);
502 focusEvent.setChild(actionIndex);
503 QAccessible::updateAccessibility(&focusEvent);
504 }
505#endif // QT_CONFIG(accessibility)
506 }
507}
508
517{
518 if (!option || !action)
519 return;
520 Q_D(const QMenuBar);
521 option->palette = palette();
522 option->state = QStyle::State_None;
523 if (isEnabled() && action->isEnabled())
525 else
526 option->palette.setCurrentColorGroup(QPalette::Disabled);
527 option->fontMetrics = fontMetrics();
528 if (d->currentAction && d->currentAction == action) {
530 if (d->popupState && !d->closePopupMode)
532 }
533 if (hasFocus() || d->currentAction)
535 option->menuRect = rect();
536 option->menuItemType = QStyleOptionMenuItem::Normal;
538 option->text = action->text();
539 option->icon = action->icon();
540}
541
660{
661 Q_Q(QMenuBar);
663 q->setAttribute(Qt::WA_CustomWhatsThis);
664
667
668 if (platformMenuBar)
669 q->hide();
670 q->setBackgroundRole(QPalette::Button);
672 q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking, nullptr, q));
673
676 extension->hide();
677}
678
679//Gets the next action for keyboard navigation
680QAction *QMenuBarPrivate::getNextAction(const int _start, const int increment) const
681{
682 Q_Q(const QMenuBar);
683 const_cast<QMenuBarPrivate*>(this)->updateGeometries();
684 bool allowActiveAndDisabled = q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, q);
685 const int start = (_start == -1 && increment == -1) ? actions.size() : _start;
686 const int end = increment == -1 ? 0 : actions.size() - 1;
687
688 for (int i = start; i != end;) {
689 i += increment;
690 QAction *current = actions.at(i);
691 if (!actionRects.at(i).isNull() && (allowActiveAndDisabled || current->isEnabled()))
692 return current;
693 }
694
695 if (_start != -1) //let's try from the beginning or the end
696 return getNextAction(-1, increment);
697
698 return nullptr;
699}
700
705{
706 Q_D(QMenuBar);
707 d->init();
708}
709
710
715{
716 Q_D(QMenuBar);
717 delete d->platformMenuBar;
718 d->platformMenuBar = nullptr;
719}
720
728{
729 QMenu *menu = new QMenu(title, this);
731 return menu;
732}
733
741{
742 QMenu *menu = new QMenu(title, this);
743 menu->setIcon(icon);
745 return menu;
746}
747
758{
759 QAction *action = menu->menuAction();
760 addAction(action);
761 return action;
762}
763
768{
769 QAction *ret = new QAction(this);
770 ret->setSeparator(true);
771 addAction(ret);
772 return ret;
773}
774
784{
785 QAction *action = new QAction(this);
786 action->setSeparator(true);
787 insertAction(before, action);
788 return action;
789}
790
798{
799 QAction *action = menu->menuAction();
800 insertAction(before, action);
801 return action;
802}
803
809{
810 Q_D(const QMenuBar);
811 return d->currentAction;
812}
813
820{
821 Q_D(QMenuBar);
822 d->setCurrentAction(act, true, false);
823}
824
825
839{
840 QList<QAction*> acts = actions();
841 for(int i = 0; i < acts.size(); i++)
842 removeAction(acts[i]);
843}
844
858{
859 Q_D(QMenuBar);
860 d->defaultPopDown = !b;
861}
862
864{
865 Q_D(const QMenuBar);
866 return !d->defaultPopDown;
867}
868
873{
874 Q_D(QMenuBar);
875 d->itemsDirty = true;
876 d->updateGeometries();
877}
878
883{
884 Q_D(QMenuBar);
885 QPainter p(this);
886 QRegion emptyArea(rect());
887
888 //draw the items
889 for (int i = 0; i < d->actions.size(); ++i) {
890 QAction *action = d->actions.at(i);
891 QRect adjustedActionRect = d->actionRect(action);
892 if (adjustedActionRect.isEmpty() || !d->isVisible(action))
893 continue;
894 if (!e->rect().intersects(adjustedActionRect))
895 continue;
896
897 emptyArea -= adjustedActionRect;
899 initStyleOption(&opt, action);
900 opt.rect = adjustedActionRect;
901 p.setClipRect(adjustedActionRect);
903 }
904 //draw border
905 if (int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, nullptr, this)) {
906 QRegion borderReg;
907 borderReg += QRect(0, 0, fw, height()); //left
908 borderReg += QRect(width()-fw, 0, fw, height()); //right
909 borderReg += QRect(0, 0, width(), fw); //top
910 borderReg += QRect(0, height()-fw, width(), fw); //bottom
911 p.setClipRegion(borderReg);
912 emptyArea -= borderReg;
914 frame.rect = rect();
920 }
921 p.setClipRegion(emptyArea);
922 QStyleOptionMenuItem menuOpt;
923 menuOpt.palette = palette();
924 menuOpt.state = QStyle::State_None;
927 menuOpt.rect = rect();
928 menuOpt.menuRect = rect();
929 style()->drawControl(QStyle::CE_MenuBarEmptyArea, &menuOpt, &p, this);
930}
931
935void QMenuBar::setVisible(bool visible)
936{
937 if (isNativeMenuBar()) {
938 if (!visible)
939 QWidget::setVisible(false);
940 return;
941 }
943}
944
949{
950 Q_D(QMenuBar);
951 if (e->button() != Qt::LeftButton)
952 return;
953
954 d->mouseDown = true;
955
956 QAction *action = d->actionAt(e->position().toPoint());
957 if (!action || !d->isVisible(action) || !action->isEnabled()) {
958 d->setCurrentAction(nullptr);
959#if QT_CONFIG(whatsthis)
961 QWhatsThis::showText(e->globalPosition().toPoint(), d->whatsThis, this);
962#endif
963 return;
964 }
965
966 if (d->currentAction == action && d->popupState) {
967 if (QMenu *menu = d->activeMenu) {
968 d->activeMenu = nullptr;
970 menu->hide();
971 }
972 } else {
973 d->setCurrentAction(action, true);
974 }
975}
976
981{
982 Q_D(QMenuBar);
983 if (e->button() != Qt::LeftButton || !d->mouseDown)
984 return;
985
986 d->mouseDown = false;
987 QAction *action = d->actionAt(e->position().toPoint());
988
989 // do noting if the action is hidden
990 if (!d->isVisible(action))
991 return;
992 if ((d->closePopupMode && action == d->currentAction) || !action || !action->menu()) {
993 //we set the current action before activating
994 //so that we let the leave event set the current back to 0
995 d->setCurrentAction(action, false);
996 if (action)
997 d->activateAction(action, QAction::Trigger);
998 }
999 d->closePopupMode = 0;
1000}
1001
1006{
1007 Q_D(QMenuBar);
1008 d->updateGeometries();
1009 int key = e->key();
1010 if (isRightToLeft()) { // in reverse mode open/close key for submenues are reversed
1011 if (key == Qt::Key_Left)
1013 else if (key == Qt::Key_Right)
1014 key = Qt::Key_Left;
1015 }
1016 if (key == Qt::Key_Tab) //means right
1018 else if (key == Qt::Key_Backtab) //means left
1019 key = Qt::Key_Left;
1020
1021 bool key_consumed = false;
1022 switch(key) {
1023 case Qt::Key_Up:
1024 case Qt::Key_Down:
1025 case Qt::Key_Enter:
1026 case Qt::Key_Space:
1027 case Qt::Key_Return: {
1028 if (!style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, nullptr, this) || !d->currentAction)
1029 break;
1030 if (d->currentAction->menu()) {
1031 d->popupAction(d->currentAction, true);
1032 } else if (key == Qt::Key_Enter || key == Qt::Key_Return || key == Qt::Key_Space) {
1033 d->activateAction(d->currentAction, QAction::Trigger);
1034 d->setCurrentAction(d->currentAction, false);
1035 d->setKeyboardMode(false);
1036 }
1037 key_consumed = true;
1038 break; }
1039
1040 case Qt::Key_Right:
1041 case Qt::Key_Left: {
1042 if (d->currentAction) {
1043 int index = d->actions.indexOf(d->currentAction);
1044 if (QAction *nextAction = d->getNextAction(index, key == Qt::Key_Left ? -1 : +1)) {
1045 d->setCurrentAction(nextAction, d->popupState, true);
1046 key_consumed = true;
1047 }
1048 }
1049 break; }
1050
1051 default:
1052 key_consumed = false;
1053 }
1054
1055#ifndef QT_NO_SHORTCUT
1056 if (!key_consumed && e->matches(QKeySequence::Cancel)) {
1057 d->setCurrentAction(nullptr);
1058 d->setKeyboardMode(false);
1059 key_consumed = true;
1060 }
1061#endif
1062
1063 if (!key_consumed &&
1064 (!e->modifiers() ||
1065 (e->modifiers()&(Qt::MetaModifier|Qt::AltModifier))) && e->text().size()==1 && !d->popupState) {
1066 int clashCount = 0;
1067 QAction *first = nullptr, *currentSelected = nullptr, *firstAfterCurrent = nullptr;
1068 {
1069 const QChar c = e->text().at(0).toUpper();
1070 for(int i = 0; i < d->actions.size(); ++i) {
1071 if (d->actionRects.at(i).isNull())
1072 continue;
1073 QAction *act = d->actions.at(i);
1074 QString s = act->text();
1075 if (!s.isEmpty()) {
1076 qsizetype ampersand = s.indexOf(u'&');
1077 if (ampersand >= 0) {
1078 if (s[ampersand+1].toUpper() == c) {
1079 clashCount++;
1080 if (!first)
1081 first = act;
1082 if (act == d->currentAction)
1083 currentSelected = act;
1084 else if (!firstAfterCurrent && currentSelected)
1085 firstAfterCurrent = act;
1086 }
1087 }
1088 }
1089 }
1090 }
1091 QAction *next_action = nullptr;
1092 if (clashCount >= 1) {
1093 if (clashCount == 1 || !d->currentAction || (currentSelected && !firstAfterCurrent))
1094 next_action = first;
1095 else
1096 next_action = firstAfterCurrent;
1097 }
1098 if (next_action) {
1099 key_consumed = true;
1100 d->setCurrentAction(next_action, true, true);
1101 }
1102 }
1103 if (key_consumed)
1104 e->accept();
1105 else
1106 e->ignore();
1107}
1108
1113{
1114 Q_D(QMenuBar);
1115 if (!(e->buttons() & Qt::LeftButton)) {
1116 d->mouseDown = false;
1117 // We receive mouse move and mouse press on touch.
1118 // Mouse move will open the menu and mouse press
1119 // will close it, so ignore mouse move.
1120 if (e->source() != Qt::MouseEventNotSynthesized)
1121 return;
1122 }
1123
1124 bool popupState = d->popupState || d->mouseDown;
1125 QAction *action = d->actionAt(e->position().toPoint());
1126 if ((action && d->isVisible(action)) || !popupState)
1127 d->setCurrentAction(action, popupState);
1128}
1129
1134{
1135 Q_D(QMenuBar);
1136 if ((!hasFocus() && !d->popupState) ||
1137 (d->currentAction && d->currentAction->menu() == nullptr))
1138 d->setCurrentAction(nullptr);
1139}
1140
1142{
1143 if (!action || !action->menu())
1144 return nullptr;
1145
1146 QPlatformMenu *platformMenu = action->menu()->platformMenu();
1147 if (!platformMenu && platformMenuBar) {
1149 if (platformMenu)
1150 action->menu()->setPlatformMenu(platformMenu);
1151 }
1152
1153 return platformMenu;
1154}
1155
1157{
1158 Q_Q(QMenuBar);
1159 QPlatformMenu *beforeMenu = nullptr;
1160 for (int beforeIndex = indexOf(const_cast<QAction *>(action)) + 1;
1161 !beforeMenu && (beforeIndex < q->actions().size());
1162 ++beforeIndex) {
1163 beforeMenu = getPlatformMenu(q->actions().at(beforeIndex));
1164 }
1165
1166 return beforeMenu;
1167}
1168
1170{
1171 const auto tag = reinterpret_cast<quintptr>(action);
1172 if (menu->tag() != tag)
1173 menu->setTag(tag);
1174 menu->setText(action->text());
1175 menu->setVisible(action->isVisible());
1176 menu->setEnabled(action->isEnabled());
1177}
1178
1183{
1184 Q_D(QMenuBar);
1185 d->itemsDirty = true;
1186
1187 if (d->platformMenuBar) {
1188 QPlatformMenuBar *nativeMenuBar = d->platformMenuBar;
1189 if (!nativeMenuBar)
1190 return;
1191
1192 auto action = static_cast<QAction *>(e->action());
1193 if (e->type() == QEvent::ActionAdded) {
1194 QPlatformMenu *menu = d->getPlatformMenu(action);
1195 if (menu) {
1196 d->copyActionToPlatformMenu(action, menu);
1197
1198 QPlatformMenu *beforeMenu = d->findInsertionPlatformMenu(action);
1199 d->platformMenuBar->insertMenu(menu, beforeMenu);
1200 }
1201 } else if (e->type() == QEvent::ActionRemoved) {
1202 QPlatformMenu *menu = d->getPlatformMenu(action);
1203 if (menu)
1204 d->platformMenuBar->removeMenu(menu);
1205 } else if (e->type() == QEvent::ActionChanged) {
1206 QPlatformMenu *cur = d->platformMenuBar->menuForTag(reinterpret_cast<quintptr>(e->action()));
1207 QPlatformMenu *menu = d->getPlatformMenu(action);
1208
1209 // the menu associated with the action can change, need to
1210 // remove and/or insert the new platform menu
1211 if (menu != cur) {
1212 if (cur)
1213 d->platformMenuBar->removeMenu(cur);
1214 if (menu) {
1215 d->copyActionToPlatformMenu(action, menu);
1216
1217 QPlatformMenu *beforeMenu = d->findInsertionPlatformMenu(action);
1218 d->platformMenuBar->insertMenu(menu, beforeMenu);
1219 }
1220 } else if (menu) {
1221 d->copyActionToPlatformMenu(action, menu);
1222 d->platformMenuBar->syncMenu(menu);
1223 }
1224 }
1225 }
1226
1227 if (e->type() == QEvent::ActionAdded) {
1228 connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
1229 connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered()));
1230 } else if (e->type() == QEvent::ActionRemoved) {
1231 e->action()->disconnect(this);
1232 }
1233 // updateGeometries() is also needed for native menu bars because
1234 // it updates shortcutIndexMap
1235 if (isVisible() || isNativeMenuBar())
1236 d->updateGeometries();
1237 if (isVisible())
1238 update();
1239}
1240
1245{
1246 Q_D(QMenuBar);
1247 if (d->keyboardState)
1248 d->focusFirstAction();
1249}
1250
1255{
1256 Q_D(QMenuBar);
1257 if (!d->popupState) {
1258 d->setCurrentAction(nullptr);
1259 d->setKeyboardMode(false);
1260 }
1261}
1262
1267{
1268 Q_D(QMenuBar);
1269 if (e->timerId() == d->autoReleaseTimer.timerId()) {
1270 d->autoReleaseTimer.stop();
1271 d->setCurrentAction(nullptr);
1272 }
1274}
1275
1276
1278{
1279 Q_Q(QMenuBar);
1280 QWidget *newParent = q->parentWidget();
1281
1282 //Note: if parent is reparented, then window may change even if parent doesn't.
1283 // We need to install an avent filter on each parent up to the parent that is
1284 // also a window (for shortcuts)
1285 QWidget *newWindow = newParent ? newParent->window() : nullptr;
1286
1287 QList<QPointer<QWidget>> newParents;
1288 // Remove event filters on ex-parents, keep them on still-parents
1289 // The parents are always ordered in the vector
1290 foreach (const QPointer<QWidget> &w, oldParents) {
1291 if (w) {
1292 if (newParent == w) {
1293 newParents.append(w);
1294 if (newParent != newWindow) //stop at the window
1295 newParent = newParent->parentWidget();
1296 } else {
1298 }
1299 }
1300 }
1301
1302 // At this point, newParent is the next one to be added to newParents
1303 while (newParent && newParent != newWindow) {
1304 //install event filters all the way up to (excluding) the window
1305 newParents.append(newParent);
1306 newParent->installEventFilter(q);
1307 newParent = newParent->parentWidget();
1308 }
1309
1310 if (newParent && newWindow) {
1311 // Install the event filter on the window
1312 newParents.append(newParent);
1313 newParent->installEventFilter(q);
1314 }
1315 oldParents = newParents;
1316
1317 if (platformMenuBar) {
1318 if (newWindow) {
1319 // force the underlying platform window to be created, since
1320 // the platform menubar needs it (and we have no other way to
1321 // discover when the platform window is created)
1322 newWindow->createWinId();
1324 } else {
1326 }
1327 }
1328}
1329
1334{
1335 Q_D(QMenuBar);
1336 if (e->type() == QEvent::StyleChange) {
1337 d->itemsDirty = true;
1338 setMouseTracking(style()->styleHint(QStyle::SH_MenuBar_MouseTracking, nullptr, this));
1339 if (parentWidget())
1341 d->updateGeometries();
1342 } else if (e->type() == QEvent::ParentChange) {
1343 d->handleReparent();
1344 } else if (e->type() == QEvent::FontChange
1345 || e->type() == QEvent::ApplicationFontChange) {
1346 d->itemsDirty = true;
1347 d->updateGeometries();
1348 }
1349
1351}
1352
1357{
1358 Q_D(QMenuBar);
1359 switch (e->type()) {
1360 case QEvent::KeyPress: {
1361 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
1362#if 0
1363 if (!d->keyboardState) { //all keypresses..
1364 d->setCurrentAction(0);
1365 return ;
1366 }
1367#endif
1368 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
1369 keyPressEvent(ke);
1370 return true;
1371 }
1372
1373 } break;
1374#ifndef QT_NO_SHORTCUT
1375 case QEvent::Shortcut: {
1376 QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
1377 int shortcutId = se->shortcutId();
1378 for(int j = 0; j < d->shortcutIndexMap.size(); ++j) {
1379 if (shortcutId == d->shortcutIndexMap.value(j))
1380 d->_q_internalShortcutActivated(j);
1381 }
1382 } break;
1383#endif
1384 case QEvent::Show:
1385 d->_q_updateLayout();
1386 break;
1387#ifndef QT_NO_SHORTCUT
1389 QKeyEvent *kev = static_cast<QKeyEvent *>(e);
1390 //we only filter out escape if there is a current action
1391 if (kev->matches(QKeySequence::Cancel) && d->currentAction) {
1392 e->accept();
1393 return true;
1394 }
1395 }
1396 break;
1397#endif
1398#if QT_CONFIG(whatsthis)
1400 e->setAccepted(d->whatsThis.size());
1401 if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) {
1402 if (action->whatsThis().size() || action->menu())
1403 e->accept();
1404 }
1405 return true;
1406#endif
1408 d->_q_updateLayout();
1409 break;
1410 default:
1411 break;
1412 }
1413 return QWidget::event(e);
1414}
1415
1420{
1421 Q_D(QMenuBar);
1422 if (object && (event->type() == QEvent::ParentChange)) //GrandparentChange
1423 d->handleReparent();
1424
1425 if (object == d->leftWidget || object == d->rightWidget) {
1426 switch (event->type()) {
1429 d->_q_updateLayout();
1430 break;
1431 default:
1432 break;
1433 }
1434 }
1435
1436 if (isNativeMenuBar() && event->type() == QEvent::ShowToParent) {
1437 // On some desktops like Unity, the D-Bus menu bar is unregistered
1438 // when the window is hidden. So when the window is shown, we need
1439 // to forcefully re-register it. The only way to force re-registering
1440 // with D-Bus menu is the handleReparent method.
1442 QWindow *handle = widget ? widget->windowHandle() : nullptr;
1443 if (handle != nullptr)
1444 d->platformMenuBar->handleReparent(handle);
1445 }
1446
1447 if (style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, nullptr, this)) {
1448 if (d->altPressed) {
1449 switch (event->type()) {
1450 case QEvent::KeyPress:
1451 case QEvent::KeyRelease:
1452 {
1453 QKeyEvent *kev = static_cast<QKeyEvent*>(event);
1454 if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) {
1455 if (event->type() == QEvent::KeyPress) // Alt-press does not interest us, we have the shortcut-override event
1456 break;
1457 d->setKeyboardMode(!d->keyboardState);
1458 }
1459 }
1460 Q_FALLTHROUGH();
1463 case QEvent::MouseMove:
1464 case QEvent::FocusIn:
1465 case QEvent::FocusOut:
1467 case QEvent::Shortcut:
1468 d->altPressed = false;
1469 qApp->removeEventFilter(this);
1470 break;
1471 default:
1472 break;
1473 }
1474 } else if (isVisible()) {
1475 if (event->type() == QEvent::ShortcutOverride) {
1476 QKeyEvent *kev = static_cast<QKeyEvent*>(event);
1477 if ((kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta)
1478 && kev->modifiers() == Qt::AltModifier) {
1479 d->altPressed = true;
1480 qApp->installEventFilter(this);
1481 }
1482 }
1483 }
1484 }
1485
1486 return false;
1487}
1488
1496{
1497 Q_D(const QMenuBar);
1498 return d->actionAt(pt);
1499}
1500
1507{
1508 Q_D(const QMenuBar);
1509 return d->actionRect(act);
1510}
1511
1516{
1517 Q_D(const QMenuBar);
1518 const bool as_gui_menubar = !isNativeMenuBar();
1519
1521 QSize ret(0, 0);
1522 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1523 const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, nullptr, this);
1524 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, nullptr, this);
1525 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, nullptr, this);
1526 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, nullptr, this);
1527 if (as_gui_menubar) {
1529 d->calcActionRects(w - (2 * fw), 0);
1530 for (int i = 0; ret.isNull() && i < d->actions.size(); ++i)
1531 ret = d->actionRects.at(i).size();
1532 if (!d->extension->isHidden())
1533 ret += QSize(d->extension->sizeHint().width(), 0);
1534 ret += QSize(2*fw + hmargin, 2*fw + vmargin);
1535 }
1536 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1537 if (d->leftWidget) {
1538 QSize sz = d->leftWidget->minimumSizeHint();
1539 ret.setWidth(ret.width() + sz.width());
1540 if (sz.height() + margin > ret.height())
1541 ret.setHeight(sz.height() + margin);
1542 }
1543 if (d->rightWidget) {
1544 QSize sz = d->rightWidget->minimumSizeHint();
1545 ret.setWidth(ret.width() + sz.width());
1546 if (sz.height() + margin > ret.height())
1547 ret.setHeight(sz.height() + margin);
1548 }
1549 if (as_gui_menubar) {
1551 opt.rect = rect();
1552 opt.menuRect = rect();
1554 opt.menuItemType = QStyleOptionMenuItem::Normal;
1556 opt.palette = palette();
1557 return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, ret, this);
1558 }
1559 return ret;
1560}
1561
1566{
1567 Q_D(const QMenuBar);
1568 const bool as_gui_menubar = !isNativeMenuBar();
1569
1571 QSize ret(0, 0);
1572 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1573 const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, nullptr, this);
1574 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, nullptr, this);
1575 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, nullptr, this);
1576 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, nullptr, this);
1577 if (as_gui_menubar) {
1579 d->calcActionRects(w - (2 * fw), 0);
1580 for (int i = 0; i < d->actionRects.size(); ++i) {
1581 const QRect &actionRect = d->actionRects.at(i);
1582 ret = ret.expandedTo(QSize(actionRect.x() + actionRect.width(), actionRect.y() + actionRect.height()));
1583 }
1584 //the action geometries already contain the top and left
1585 //margins. So we only need to add those from right and bottom.
1586 ret += QSize(fw + hmargin, fw + vmargin);
1587 }
1588 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1589 if (d->leftWidget) {
1590 QSize sz = d->leftWidget->sizeHint();
1591 sz.rheight() += margin;
1592 ret = ret.expandedTo(sz);
1593 }
1594 if (d->rightWidget) {
1595 QSize sz = d->rightWidget->sizeHint();
1596 ret.setWidth(ret.width() + sz.width());
1597 if (sz.height() + margin > ret.height())
1598 ret.setHeight(sz.height() + margin);
1599 }
1600 if (as_gui_menubar) {
1602 opt.rect = rect();
1603 opt.menuRect = rect();
1605 opt.menuItemType = QStyleOptionMenuItem::Normal;
1607 opt.palette = palette();
1608 return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, ret, this);
1609 }
1610 return ret;
1611}
1612
1617{
1618 Q_D(const QMenuBar);
1619 const bool as_gui_menubar = !isNativeMenuBar();
1620
1621 const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1622 int height = 0;
1623 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, nullptr, this);
1624 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, nullptr, this);
1625 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, nullptr, this);
1626 if (as_gui_menubar) {
1627 for (int i = 0; i < d->actionRects.size(); ++i)
1628 height = qMax(height, d->actionRects.at(i).height());
1629 if (height) //there is at least one non-null item
1630 height += spaceBelowMenuBar;
1631 height += 2*fw;
1632 height += 2*vmargin;
1633 }
1634 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1635 if (d->leftWidget)
1636 height = qMax(d->leftWidget->sizeHint().height() + margin, height);
1637 if (d->rightWidget)
1638 height = qMax(d->rightWidget->sizeHint().height() + margin, height);
1639 if (as_gui_menubar) {
1641 opt.initFrom(this);
1642 opt.menuRect = rect();
1644 opt.menuItemType = QStyleOptionMenuItem::Normal;
1646 return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, QSize(0, height), this).height(); //not pretty..
1647 }
1648 return height;
1649}
1650
1655{
1656 Q_Q(QMenuBar);
1657 QAction *act = actions.at(id);
1658 if (act && act->menu()) {
1659 if (QPlatformMenu *platformMenu = act->menu()->platformMenu()) {
1660 platformMenu->showPopup(q->windowHandle(), actionRects.at(id), nullptr);
1661 return;
1662 }
1663 }
1664
1666 setCurrentAction(act, true, true);
1667 if (act && !act->menu()) {
1669 //100 is the same as the default value in QPushButton::animateClick
1670 autoReleaseTimer.start(100, q);
1671 } else if (act && q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, nullptr, q)) {
1672 // When we open a menu using a shortcut, we should end up in keyboard state
1673 setKeyboardMode(true);
1674 }
1675}
1676
1678{
1679 Q_Q(QMenuBar);
1680 itemsDirty = true;
1681 if (q->isVisible()) {
1683 q->update();
1684 }
1685}
1686
1701{
1702 Q_D(QMenuBar);
1703 switch (corner) {
1704 case Qt::TopLeftCorner:
1705 if (d->leftWidget)
1706 d->leftWidget->removeEventFilter(this);
1707 d->leftWidget = w;
1708 break;
1709 case Qt::TopRightCorner:
1710 if (d->rightWidget)
1711 d->rightWidget->removeEventFilter(this);
1712 d->rightWidget = w;
1713 break;
1714 default:
1715 qWarning("QMenuBar::setCornerWidget: Only TopLeftCorner and TopRightCorner are supported");
1716 return;
1717 }
1718
1719 if (w) {
1720 w->setParent(this);
1721 w->installEventFilter(this);
1722 }
1723
1724 d->_q_updateLayout();
1725}
1726
1735{
1736 Q_D(const QMenuBar);
1737 QWidget *w = nullptr;
1738 switch(corner) {
1739 case Qt::TopLeftCorner:
1740 w = d->leftWidget;
1741 break;
1742 case Qt::TopRightCorner:
1743 w = d->rightWidget;
1744 break;
1745 default:
1746 qWarning("QMenuBar::cornerWidget: Only TopLeftCorner and TopRightCorner are supported");
1747 break;
1748 }
1749
1750 return w;
1751}
1752
1770void QMenuBar::setNativeMenuBar(bool nativeMenuBar)
1771{
1772 Q_D(QMenuBar);
1773 if (nativeMenuBar != bool(d->platformMenuBar)) {
1774 if (!nativeMenuBar) {
1775 delete d->platformMenuBar;
1776 d->platformMenuBar = nullptr;
1777 d->itemsDirty = true;
1778 } else {
1779 if (!d->platformMenuBar)
1781 }
1782
1784 if (!nativeMenuBar && parentWidget())
1785 setVisible(true);
1786 }
1787}
1788
1790{
1791 Q_D(const QMenuBar);
1792 return bool(d->platformMenuBar);
1793}
1794
1799{
1800 Q_D(const QMenuBar);
1801 return d->platformMenuBar;
1802}
1803
1832// for private slots
1833
1835
1836#include <moc_qmenubar.cpp>
void setIcon(const QIcon &icon)
The QActionEvent class provides an event that is generated when a QAction is added,...
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
ActionEvent
This enum type is used when calling QAction::activate()
Definition qaction.h:175
@ Trigger
Definition qaction.h:175
@ Hover
Definition qaction.h:175
T menu() const
Returns the menu contained by this action.
Definition qaction.h:186
bool isSeparator() const
Returns true if this action is a separator action; otherwise it returns false.
Definition qaction.cpp:585
bool isVisible() const
Definition qaction.cpp:1018
void setSeparator(bool b)
If b is true then this action will be considered a separator.
Definition qaction.cpp:569
bool showStatusText(QObject *object=nullptr)
Updates the relevant status bar for the UI represented by object by sending a QStatusTipEvent.
Definition qaction.cpp:736
void activate(ActionEvent event)
Sends the relevant signals for ActionEvent event.
Definition qaction.cpp:1082
QString text
the action's descriptive text
Definition qaction.h:39
QIcon icon
the action's icon
Definition qaction.h:38
bool isEnabled() const
Definition qaction.cpp:971
static QWidget * focusWidget()
Returns the application widget that has the keyboard input focus, or \nullptr if no widget in this ap...
static QWidget * activePopupWidget()
Returns the active popup widget.
void start(int msec, QObject *obj)
\obsolete Use chrono overload instead.
void stop()
Stops the timer.
\inmodule QtCore
Definition qchar.h:48
void showPopup(const QWindow *parentWindow, const QRect &targetRect, const QPlatformMenuItem *item) override
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
static bool testAttribute(Qt::ApplicationAttribute attribute)
Returns true if attribute attribute is set; otherwise returns false.
\inmodule QtCore
Definition qcoreevent.h:45
@ QueryWhatsThis
Definition qcoreevent.h:169
@ ActionRemoved
Definition qcoreevent.h:153
@ ParentChange
Definition qcoreevent.h:80
@ ActionAdded
Definition qcoreevent.h:152
@ LayoutDirectionChange
Definition qcoreevent.h:124
@ ShortcutOverride
Definition qcoreevent.h:158
@ FocusOut
Definition qcoreevent.h:67
@ StyleChange
Definition qcoreevent.h:136
@ ActionChanged
Definition qcoreevent.h:151
@ FontChange
Definition qcoreevent.h:133
@ KeyRelease
Definition qcoreevent.h:65
@ MouseMove
Definition qcoreevent.h:63
@ KeyPress
Definition qcoreevent.h:64
@ FocusIn
Definition qcoreevent.h:66
@ ActivationChange
Definition qcoreevent.h:135
@ MouseButtonPress
Definition qcoreevent.h:60
@ HideToParent
Definition qcoreevent.h:86
@ ApplicationFontChange
Definition qcoreevent.h:91
@ ShowToParent
Definition qcoreevent.h:85
@ MouseButtonRelease
Definition qcoreevent.h:61
The QFocusEvent class contains event parameters for widget focus events.
Definition qevent.h:469
\reentrant \inmodule QtGui
QSize size(int flags, const QString &str, int tabstops=0, int *tabarray=nullptr) const
Returns the size in pixels of text.
int midLineWidth
the width of the mid-line
Definition qframe.h:23
int lineWidth
the line width
Definition qframe.h:22
static QPlatformTheme * platformTheme()
QScreen * primaryScreen
the primary (or default) screen of the application.
The QHelpEvent class provides an event that is used to request helpful information about a particular...
Definition qevent.h:787
const QPoint & pos() const
Returns the mouse cursor position when the event was generated, relative to the widget to which the e...
Definition qevent.h:797
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition qicon.cpp:973
The QKeyEvent class describes a key event.
Definition qevent.h:423
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately after the event occurred.
Definition qevent.cpp:1465
int key() const
Returns the code of the key that was pressed or released.
Definition qevent.h:433
static QKeySequence mnemonic(const QString &text)
Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics ...
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
QList< T > & fill(parameter_type t, qsizetype size=-1)
Definition qlist.h:896
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
T value(qsizetype i) const
Definition qlist.h:661
void reserve(qsizetype size)
Definition qlist.h:746
void resize(qsizetype size)
Definition qlist.h:392
void append(parameter_type t)
Definition qlist.h:441
void clear()
Definition qlist.h:417
QSize sizeHint() const override
Definition qmenubar.cpp:71
void paintEvent(QPaintEvent *) override
\reimp
Definition qmenubar.cpp:60
QMenuBarExtension(QWidget *parent)
Definition qmenubar.cpp:49
QPlatformMenu * findInsertionPlatformMenu(const QAction *action)
QMenuBarExtension * extension
Definition qmenubar_p.h:81
QPlatformMenu * getPlatformMenu(const QAction *action)
QPointer< QAction > currentAction
Definition qmenubar_p.h:55
QAction * actionAt(QPoint p) const
Definition qmenubar.cpp:81
void calcActionRects(int max_width, int start) const
Definition qmenubar.cpp:373
void _q_internalShortcutActivated(int)
void _q_actionTriggered()
Definition qmenubar.cpp:485
QList< QRect > actionRects
Definition qmenubar_p.h:49
void _q_updateLayout()
QPointer< QMenu > activeMenu
Definition qmenubar_p.h:63
void copyActionToPlatformMenu(const QAction *e, QPlatformMenu *menu)
void focusFirstAction()
Definition qmenubar.cpp:238
QAction * getNextAction(const int start, const int increment) const
Definition qmenubar.cpp:680
QPlatformMenuBar * platformMenuBar
Definition qmenubar_p.h:98
void popupAction(QAction *, bool)
Definition qmenubar.cpp:275
void setCurrentAction(QAction *, bool=false, bool=false)
Definition qmenubar.cpp:330
QPointer< QWidget > keyboardFocusWidget
Definition qmenubar_p.h:69
QPointer< QWidget > rightWidget
Definition qmenubar_p.h:80
QList< QPointer< QWidget > > oldParents
Definition qmenubar_p.h:91
int indexOf(QAction *act) const
Definition qmenubar_p.h:103
QBasicTimer autoReleaseTimer
Definition qmenubar_p.h:97
void activateAction(QAction *, QAction::ActionEvent)
Definition qmenubar.cpp:469
QPointer< QWidget > leftWidget
Definition qmenubar_p.h:80
QList< QAction * > hiddenActions
Definition qmenubar_p.h:93
void updateGeometries()
Definition qmenubar.cpp:129
void handleReparent()
QRect menuRect(bool) const
Definition qmenubar.cpp:90
bool isVisible(QAction *action)
Definition qmenubar.cpp:124
void _q_actionHovered()
Definition qmenubar.cpp:493
void setKeyboardMode(bool)
Definition qmenubar.cpp:249
QRect actionRect(QAction *) const
Definition qmenubar.cpp:225
QList< int > shortcutIndexMap
Definition qmenubar_p.h:48
The QMenuBar class provides a horizontal menu bar.
Definition qmenubar.h:20
QWidget * cornerWidget(Qt::Corner corner=Qt::TopRightCorner) const
Returns the widget on the left of the first or on the right of the last menu item,...
void hovered(QAction *action)
This signal is emitted when a menu action is highlighted; action is the action that caused the event ...
void setActiveAction(QAction *action)
Definition qmenubar.cpp:819
QAction * addMenu(QMenu *menu)
Appends menu to the menu bar.
Definition qmenubar.cpp:757
QMenuBar(QWidget *parent=nullptr)
Constructs a menu bar with parent parent.
Definition qmenubar.cpp:704
void setVisible(bool visible) override
\reimp
Definition qmenubar.cpp:935
QAction * actionAt(const QPoint &) const
Returns the QAction at pt.
void setNativeMenuBar(bool nativeMenuBar)
virtual void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
Initialize option with the values from the menu bar and information from action.
Definition qmenubar.cpp:516
bool event(QEvent *) override
\reimp
void leaveEvent(QEvent *) override
\reimp
QSize sizeHint() const override
\reimp
bool nativeMenuBar
Whether or not a menubar will be used as a native menubar on platforms that support it.
Definition qmenubar.h:24
void focusInEvent(QFocusEvent *) override
\reimp
void changeEvent(QEvent *) override
\reimp
void focusOutEvent(QFocusEvent *) override
\reimp
void mousePressEvent(QMouseEvent *) override
\reimp
Definition qmenubar.cpp:948
void timerEvent(QTimerEvent *) override
\reimp
void mouseMoveEvent(QMouseEvent *) override
\reimp
bool isNativeMenuBar() const
void setCornerWidget(QWidget *w, Qt::Corner corner=Qt::TopRightCorner)
This sets the given widget to be shown directly on the left of the first menu item,...
void paintEvent(QPaintEvent *) override
\reimp
Definition qmenubar.cpp:882
QRect actionGeometry(QAction *) const
Returns the geometry of action act as a QRect.
QAction * insertSeparator(QAction *before)
This convenience function creates a new separator action, i.e.
Definition qmenubar.cpp:783
int heightForWidth(int) const override
\reimp
QPlatformMenuBar * platformMenuBar()
QAction * addSeparator()
Appends a separator to the menu.
Definition qmenubar.cpp:767
void clear()
Removes all the actions from the menu bar.
Definition qmenubar.cpp:838
QAction * activeAction() const
Returns the QAction that is currently highlighted, if any, else \nullptr.
Definition qmenubar.cpp:808
bool eventFilter(QObject *, QEvent *) override
\reimp
~QMenuBar()
Destroys the menu bar.
Definition qmenubar.cpp:714
void resizeEvent(QResizeEvent *) override
\reimp
Definition qmenubar.cpp:872
void actionEvent(QActionEvent *) override
\reimp
void setDefaultUp(bool)
Definition qmenubar.cpp:857
QSize minimumSizeHint() const override
\reimp
void mouseReleaseEvent(QMouseEvent *) override
\reimp
Definition qmenubar.cpp:980
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3124
void keyPressEvent(QKeyEvent *) override
\reimp
void triggered(QAction *action)
This signal is emitted when an action in a menu belonging to this menubar is triggered as a result of...
bool isDefaultUp() const
Definition qmenubar.cpp:863
QAction * insertMenu(QAction *before, QMenu *menu)
This convenience function inserts menu before action before and returns the menus menuAction().
Definition qmenubar.cpp:797
static QMenuPrivate * get(QMenu *m)
Definition qmenu_p.h:307
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
QSize sizeHint() const override
\reimp
Definition qmenu.cpp:2247
void clear()
Removes all the menu's actions.
Definition qmenu.cpp:2203
void setIcon(const QIcon &icon)
Definition qmenu.cpp:1094
void popup(const QPoint &pos, QAction *at=nullptr)
Displays the menu so that the action atAction will be at the specified global position p.
Definition qmenu.cpp:2288
QAction * menuAction() const
Returns the action associated with this menu.
Definition qmenu.cpp:1047
\inmodule QtGui
Definition qevent.h:195
\inmodule QtCore
Definition qobject.h:90
void installEventFilter(QObject *filterObj)
Installs an event filter filterObj on this object.
Definition qobject.cpp:2269
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
void removeEventFilter(QObject *obj)
Removes an event filter object obj from this object.
Definition qobject.cpp:2300
virtual void timerEvent(QTimerEvent *event)
This event handler can be reimplemented in a subclass to receive timer events for the object.
Definition qobject.cpp:1433
Q_WEAK_OVERLOAD void setObjectName(const QString &name)
Sets the object's name to name.
Definition qobject.h:114
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:485
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
@ Disabled
Definition qpalette.h:48
virtual QPlatformMenu * createMenu() const
virtual void handleReparent(QWindow *newParentWindow)=0
virtual QPlatformMenuBar * createPlatformMenuBar() const
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore
Definition qpointer.h:18
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr bool isEmpty() const noexcept
Returns true if the rectangle is empty, otherwise returns false.
Definition qrect.h:166
constexpr void adjust(int x1, int y1, int x2, int y2) noexcept
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
Definition qrect.h:372
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:238
constexpr bool isNull() const noexcept
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition qrect.h:163
constexpr int bottom() const noexcept
Returns the y-coordinate of the rectangle's bottom edge.
Definition qrect.h:181
constexpr int top() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:175
bool contains(const QRect &r, bool proper=false) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrect.cpp:851
constexpr int left() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:172
constexpr int x() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:184
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:235
constexpr int y() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:187
constexpr int right() const noexcept
Returns the x-coordinate of the rectangle's right edge.
Definition qrect.h:178
The QRegion class specifies a clip region for a painter.
Definition qregion.h:27
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:547
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
QRect geometry
the screen's geometry in pixels
Definition qscreen.h:45
QRect virtualGeometry
the pixel geometry of the virtual desktop to which this screen belongs
Definition qscreen.h:47
QScreen * virtualSiblingAt(QPoint point)
Returns the screen at point within the set of \l QScreen::virtualSiblings(), or nullptr if outside of...
Definition qscreen.cpp:629
The QShortcutEvent class provides an event which is generated when the user presses a key combination...
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
constexpr int & rheight() noexcept
Returns a reference to the height.
Definition qsize.h:156
constexpr QSize expandedTo(const QSize &) const noexcept
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition qsize.h:191
constexpr bool isEmpty() const noexcept
Returns true if either of the width and height is less than or equal to 0; otherwise returns false.
Definition qsize.h:123
The QStatusTipEvent class provides an event that is used to show messages in a status bar.
Definition qevent.h:807
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString static QString qsizetype indexOf(QChar c, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4420
ButtonFeatures features
\variable QStyleOptionFocusRect::backgroundColor
\variable QStyleOptionProgressBar::minimum
MenuItemType menuItemType
\variable QStyleOptionDockWidget::title
QStyle::State state
QPalette palette
void initFrom(const QWidget *w)
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget.
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI.
Definition qstyle.h:29
@ State_Sunken
Definition qstyle.h:69
@ State_HasFocus
Definition qstyle.h:75
@ State_Enabled
Definition qstyle.h:67
@ State_Selected
Definition qstyle.h:82
@ State_None
Definition qstyle.h:66
@ CT_MenuBar
Definition qstyle.h:554
@ CT_MenuBarItem
Definition qstyle.h:553
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=nullptr) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
@ SH_MainWindow_SpaceBelowMenuBar
Definition qstyle.h:595
@ SH_MenuBar_AltKeyNavigation
Definition qstyle.h:601
@ SH_Menu_AllowActiveAndDisabled
Definition qstyle.h:597
@ SH_MenuBar_MouseTracking
Definition qstyle.h:604
@ SH_DrawMenuBarSeparator
Definition qstyle.h:630
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=nullptr, const QWidget *widget=nullptr, QStyleHintReturn *returnData=nullptr) const =0
Returns an integer representing the specified style hint for the given widget described by the provid...
@ SP_ToolBarHorizontalExtensionButton
Definition qstyle.h:742
@ CE_MenuBarItem
Definition qstyle.h:197
@ CE_MenuBarEmptyArea
Definition qstyle.h:198
static QRect visualRect(Qt::LayoutDirection direction, const QRect &boundingRect, const QRect &logicalRect)
Returns the given logicalRectangle converted to screen coordinates based on the specified direction.
Definition qstyle.cpp:2144
@ PM_MenuBarHMargin
Definition qstyle.h:460
@ PM_MenuBarPanelWidth
Definition qstyle.h:457
@ PM_SmallIconSize
Definition qstyle.h:493
@ PM_MenuBarItemSpacing
Definition qstyle.h:458
@ PM_ToolBarExtensionExtent
Definition qstyle.h:486
@ PM_MenuBarVMargin
Definition qstyle.h:459
@ CC_ToolButton
Definition qstyle.h:336
@ PE_PanelMenuBar
Definition qstyle.h:120
virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w=nullptr) const =0
Draws the given element with the provided painter with the style options specified by option.
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const =0
Returns the value of the given pixel metric.
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w=nullptr) const =0
Draws the given primitive element with the provided painter using the style options specified by opti...
\inmodule QtCore
Definition qcoreevent.h:359
The QToolButton class provides a quick-access button to commands or options, usually used inside a QT...
Definition qtoolbutton.h:20
void setAutoRaise(bool enable)
virtual void initStyleOption(QStyleOptionToolButton *option) const
Initialize option with the values from this QToolButton.
static void showText(const QPoint &pos, const QString &text, QWidget *w=nullptr)
Shows text as a "What's This?" window, at global position pos.
static bool inWhatsThisMode()
Returns true if the user interface is in "What's This?" mode; otherwise returns false.
QList< QAction * > actions
Definition qwidget_p.h:701
QTLWExtra * topData() const
Definition qwidget_p.h:818
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
QWidget * window() const
Returns the window for this widget, i.e.
Definition qwidget.cpp:4320
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:886
void updateGeometry()
Notifies the layout system that this widget has changed and may need to change geometry.
void setEnabled(bool)
Definition qwidget.cpp:3365
QPalette palette
the widget's palette
Definition qwidget.h:132
int width
the width of the widget excluding any window frame
Definition qwidget.h:114
void setMouseTracking(bool enable)
Definition qwidget.h:853
QFontMetrics fontMetrics() const
Returns the font metrics for the widget's current font.
Definition qwidget.h:847
void createWinId()
Definition qwidget.cpp:2448
QWidget * focusWidget() const
Returns the last child of this widget that setFocus had been called on.
Definition qwidget.cpp:6851
void setFocusPolicy(Qt::FocusPolicy policy)
Definition qwidget.cpp:7904
void hide()
Hides the widget.
Definition qwidget.cpp:8209
int height
the height of the widget excluding any window frame
Definition qwidget.h:115
QList< QAction * > actions() const
Returns the (possibly empty) list of this widget's actions.
Definition qwidget.cpp:3214
QRect rect
the internal geometry of the widget excluding any window frame
Definition qwidget.h:116
void setFocus()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:423
void show()
Shows the widget and its child widgets.
Definition qwidget.cpp:7956
virtual void setVisible(bool visible)
Definition qwidget.cpp:8329
void insertAction(QAction *before, QAction *action)
Inserts the action action to this widget's list of actions, before the action before.
Definition qwidget.cpp:3149
void ensurePolished() const
Ensures that the widget and its children have been polished by QStyle (i.e., have a proper font and p...
bool isEnabled() const
Definition qwidget.h:814
void update()
Updates the widget unless updates are disabled or the widget is hidden.
QWindow * windowHandle() const
If this is a native widget, return the associated QWindow.
Definition qwidget.cpp:2490
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition qwidget.cpp:9428
QSize sizeHint
the recommended size for the widget
Definition qwidget.h:148
bool isRightToLeft() const
Definition qwidget.h:419
void addActions(const QList< QAction * > &actions)
Appends the actions actions to this widget's list of actions.
Definition qwidget.cpp:3134
bool event(QEvent *event) override
This is the main event handler; it handles event event.
Definition qwidget.cpp:8912
QStyle * style() const
Definition qwidget.cpp:2607
void resize(int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:883
bool hasFocus() const
Definition qwidget.cpp:6471
QWidget * parentWidget() const
Returns the parent of this widget, or \nullptr if it does not have any parent widget.
Definition qwidget.h:904
void removeAction(QAction *action)
Removes the action action from this widget's list of actions.
Definition qwidget.cpp:3193
bool isVisible() const
Definition qwidget.h:874
bool visible
whether the widget is visible
Definition qwidget.h:144
\inmodule QtGui
Definition qwindow.h:63
QOpenGLWidget * widget
[1]
double e
rect
[4]
QStyleOptionButton opt
Combined button and popup list for selecting options.
@ TopRightCorner
@ TopLeftCorner
@ LeftButton
Definition qnamespace.h:57
@ WA_CustomWhatsThis
Definition qnamespace.h:312
@ WA_NoMouseReplay
Definition qnamespace.h:319
@ NoFocus
Definition qnamespace.h:106
@ TextShowMnemonic
Definition qnamespace.h:172
@ MouseEventNotSynthesized
@ Key_Tab
Definition qnamespace.h:659
@ Key_Return
Definition qnamespace.h:662
@ Key_Right
Definition qnamespace.h:674
@ Key_Enter
Definition qnamespace.h:663
@ Key_Space
Definition qnamespace.h:512
@ Key_Backtab
Definition qnamespace.h:660
@ Key_Left
Definition qnamespace.h:672
@ Key_Alt
Definition qnamespace.h:681
@ Key_Up
Definition qnamespace.h:673
@ Key_Down
Definition qnamespace.h:675
@ Key_Meta
Definition qnamespace.h:680
@ MetaModifier
@ AltModifier
@ AA_DontUseNativeMenuBar
Definition qnamespace.h:430
@ NoFocusReason
@ MenuBarFocusReason
NSMenu QCocoaMenu * platformMenu
#define Q_FALLTHROUGH()
#define qApp
AudioChannelLayoutTag tag
#define qWarning
Definition qlogging.h:162
return ret
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLboolean GLboolean GLboolean b
GLuint64 GLenum void * handle
GLint GLint GLint GLint GLint x
[0]
GLuint64 key
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLuint GLuint end
GLint left
GLuint start
GLint first
GLint y
struct _cl_event * event
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
GLdouble s
[6]
Definition qopenglext.h:235
GLfloat GLfloat p
[1]
GLuint GLenum option
#define emit
size_t quintptr
Definition qtypes.h:72
ptrdiff_t qsizetype
Definition qtypes.h:70
QWidget * qobject_cast< QWidget * >(QObject *o)
Definition qwidget.h:786
QtConcurrent::task([]{ qDebug("Hello, world!");}).spawn(FutureResult void increment(QPromise< int > &promise, int i)
[10]
QString title
[35]
QFrame frame
[0]
QMenu menu
[5]
qsizetype indexOf(const AT &t, qsizetype from=0) const noexcept
Definition qlist.h:955
bool contains(const AT &t) const noexcept
Definition qlist.h:44
QScreen * initialScreen
Definition qwidget_p.h:111
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent