9#if QT_CONFIG(accessibility)
15#if QT_CONFIG(mainwindow)
21#if QT_CONFIG(toolbutton)
24#if QT_CONFIG(whatsthis)
27#include <qpa/qplatformtheme.h>
28#include "private/qguiapplication_p.h"
29#include "qpa/qplatformintegration.h"
33#include <private/qscreen_p.h>
74 return QSize(ext, ext);
99 if (
q->isRightToLeft())
107 if (
q->isRightToLeft())
115 if (
q->isRightToLeft())
143 q_width -= sz.
width();
144 q_start = sz.
width();
151 q_width -= sz.
width();
159 if (
q->isNativeMenuBar()) {
166#ifndef QT_NO_SHORTCUT
173 for (
int i = 0;
i < actionsCount;
i++)
184 bool hasHiddenActions =
false;
188 hasHiddenActions =
true;
194 if (hasHiddenActions) {
214 int x =
q->isRightToLeft()
285 activeMenuPriv->causedPopup.widget =
q;
286 activeMenuPriv->causedPopup.action = 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);
299 std::swap(popupScreen, activeMenuPriv->popupScreen);
304 QPoint pos(
q->mapToGlobal(popupPos).x(), screenTestPos.y());
306 QRect screenRect = popupScreen->geometry();
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;
332 pos.setY(
qMax(screenRect.
y(),
q->mapToGlobal(
QPoint(0, adjustedActionRect.top()-popup_size.height())).
y()));
364#if QT_CONFIG(statustip)
373#if QT_CONFIG(statustip)
374 }
else if (previousAction) {
395 const QStyle *style =
q->style();
398 int max_item_height = 0, separator = -1, separator_start = 0, separator_len = 0;
423 else if (!
s.isEmpty())
429 q->initStyleOption(&
opt, action);
434 int iWidth = sz.
width() + itemSpacing;
436 separator_start += iWidth;
438 separator_len += iWidth;
441 max_item_height =
qMax(max_item_height, sz.
height());
449 int x = fw + ((
start == -1) ? hmargin :
start) + itemSpacing;
450 int y = fw + vmargin;
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;
499 if (
QAction *action = qobject_cast<QAction *>(
q->sender())) {
500 emit q->triggered(action);
507 if (
QAction *action = qobject_cast<QAction *>(
q->sender())) {
508 emit q->hovered(action);
509#if QT_CONFIG(accessibility)
510 if (QAccessible::isActive()) {
512 QAccessibleEvent focusEvent(
q, QAccessible::Focus);
513 focusEvent.setChild(actionIndex);
514 QAccessible::updateAccessibility(&focusEvent);
539 if (
d->currentAction &&
d->currentAction == action) {
541 if (
d->popupState && !
d->closePopupMode)
728 delete d->platformMenuBar;
729 d->platformMenuBar =
nullptr;
781 ret->setSeparator(
true);
822 return d->currentAction;
833 d->setCurrentAction(act,
true,
false);
851 QList<QAction*> acts =
actions();
852 for(
int i = 0;
i < acts.size();
i++)
871 d->defaultPopDown = !
b;
877 return !
d->defaultPopDown;
886 d->itemsDirty =
true;
887 d->updateGeometries();
900 for (
int i = 0;
i <
d->actions.size(); ++
i) {
902 QRect adjustedActionRect =
d->actionRect(action);
903 if (adjustedActionRect.isEmpty() || !
d->isVisible(action))
908 emptyArea -= adjustedActionRect;
912 p.setClipRect(adjustedActionRect);
922 p.setClipRegion(borderReg);
923 emptyArea -= borderReg;
932 p.setClipRegion(emptyArea);
938 menuOpt.rect =
rect();
939 menuOpt.menuRect =
rect();
969 d->setCurrentAction(
nullptr);
970#if QT_CONFIG(whatsthis)
977 if (
d->currentAction == action &&
d->popupState) {
979 d->activeMenu =
nullptr;
984 d->setCurrentAction(action,
true);
997 d->mouseDown =
false;
1001 if (!
d->isVisible(action))
1003 if ((
d->closePopupMode && action ==
d->currentAction) || !action || !action->
menu()) {
1006 d->setCurrentAction(action,
false);
1010 d->closePopupMode = 0;
1019 d->updateGeometries();
1032 bool key_consumed =
false;
1041 if (
d->currentAction->menu()) {
1042 d->popupAction(
d->currentAction,
true);
1045 d->setCurrentAction(
d->currentAction,
false);
1046 d->setKeyboardMode(
false);
1048 key_consumed =
true;
1053 if (
d->currentAction) {
1054 int index =
d->actions.indexOf(
d->currentAction);
1056 d->setCurrentAction(nextAction,
d->popupState,
true);
1057 key_consumed =
true;
1063 key_consumed =
false;
1066#ifndef QT_NO_SHORTCUT
1068 d->setCurrentAction(
nullptr);
1069 d->setKeyboardMode(
false);
1070 key_consumed =
true;
1074 if (!key_consumed &&
1078 QAction *
first =
nullptr, *currentSelected =
nullptr, *firstAfterCurrent =
nullptr;
1081 for(
int i = 0;
i <
d->actions.size(); ++
i) {
1082 if (
d->actionRects.at(
i).isNull())
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);
1127 d->mouseDown =
false;
1135 bool popupState =
d->popupState ||
d->mouseDown;
1137 if ((action &&
d->
isVisible(action)) || !popupState)
1138 d->setCurrentAction(action, popupState);
1148 (
d->currentAction &&
d->currentAction->menu() ==
nullptr))
1149 d->setCurrentAction(
nullptr);
1154 if (!action || !action->
menu())
1171 for (
int beforeIndex =
indexOf(
const_cast<QAction *
>(action)) + 1;
1172 !beforeMenu && (beforeIndex <
q->actions().
size());
1182 const auto tag =
reinterpret_cast<quintptr>(action);
1196 d->itemsDirty =
true;
1198 if (
d->platformMenuBar) {
1203 auto action =
static_cast<QAction *
>(e->action());
1207 d->copyActionToPlatformMenu(action,
menu);
1209 QPlatformMenu *beforeMenu =
d->findInsertionPlatformMenu(action);
1210 d->platformMenuBar->insertMenu(
menu, beforeMenu);
1215 d->platformMenuBar->removeMenu(
menu);
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);
1242 e->action()->disconnect(
this);
1247 d->updateGeometries();
1258 if (
d->keyboardState)
1259 d->focusFirstAction();
1268 if (!
d->popupState) {
1269 d->setCurrentAction(
nullptr);
1270 d->setKeyboardMode(
false);
1280 if (e->
timerId() ==
d->autoReleaseTimer.timerId()) {
1281 d->autoReleaseTimer.stop();
1282 d->setCurrentAction(
nullptr);
1296 QWidget *newWindow = newParent ? newParent->
window() :
nullptr;
1298 QList<QPointer<QWidget>> newParents;
1306 for (
const QPointer<QWidget> &
w :
copy) {
1308 if (newParent ==
w) {
1310 if (newParent != newWindow)
1319 while (newParent && newParent != newWindow) {
1321 newParents.append(newParent);
1326 if (newParent && newWindow) {
1328 newParents.append(newParent);
1338 newWindow->createWinId();
1353 d->itemsDirty =
true;
1357 d->updateGeometries();
1359 d->handleReparent();
1362 d->itemsDirty =
true;
1363 d->updateGeometries();
1375 switch (e->
type()) {
1379 if (!
d->keyboardState) {
1380 d->setCurrentAction(0);
1390#ifndef QT_NO_SHORTCUT
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
1414#if QT_CONFIG(whatsthis)
1418 if (action->whatsThis().size() || action->menu())
1424 d->_q_updateLayout();
1439 d->handleReparent();
1441 if (
object ==
d->leftWidget ||
object ==
d->rightWidget) {
1442 switch (
event->type()) {
1445 d->_q_updateLayout();
1460 d->platformMenuBar->handleReparent(
handle);
1464 if (
d->altPressed) {
1465 switch (
event->type()) {
1473 d->setKeyboardMode(!
d->keyboardState);
1484 d->altPressed =
false;
1485 qApp->removeEventFilter(
this);
1495 d->altPressed =
true;
1496 qApp->installEventFilter(
this);
1514 return d->actionAt(pt);
1525 return d->actionRect(act);
1543 if (as_gui_menubar) {
1545 d->calcActionRects(
w - (2 * fw), 0);
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();
1556 if (sz.
height() + margin >
ret.height())
1559 if (
d->rightWidget) {
1560 QSize sz =
d->rightWidget->minimumSizeHint();
1562 if (sz.
height() + margin >
ret.height())
1565 if (as_gui_menubar) {
1593 if (as_gui_menubar) {
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);
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();
1608 ret =
ret.expandedTo(sz);
1610 if (
d->rightWidget) {
1611 QSize sz =
d->rightWidget->sizeHint();
1613 if (sz.
height() + margin >
ret.height())
1616 if (as_gui_menubar) {
1642 if (as_gui_menubar) {
1643 for (
int i = 0;
i <
d->actionRects.size(); ++
i)
1646 height += spaceBelowMenuBar;
1650 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1655 if (as_gui_menubar) {
1674 if (act && act->
menu()) {
1683 if (act && !act->
menu()) {
1697 if (
q->isVisible()) {
1722 d->leftWidget->removeEventFilter(
this);
1727 d->rightWidget->removeEventFilter(
this);
1731 qWarning(
"QMenuBar::setCornerWidget: Only TopLeftCorner and TopRightCorner are supported");
1737 w->installEventFilter(
this);
1740 d->_q_updateLayout();
1762 qWarning(
"QMenuBar::cornerWidget: Only TopLeftCorner and TopRightCorner are supported");
1791 delete d->platformMenuBar;
1792 d->platformMenuBar =
nullptr;
1793 d->itemsDirty =
true;
1795 if (!
d->platformMenuBar)
1808 return bool(
d->platformMenuBar);
1817 return d->platformMenuBar;
1852#include <moc_qmenubar.cpp>
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...
ActionEvent
This enum type is used when calling QAction::activate()
T menu() const
Returns the menu contained by this action.
bool isSeparator() const
Returns true if this action is a separator action; otherwise it returns false.
void setSeparator(bool b)
If b is true then this action will be considered a separator.
bool showStatusText(QObject *object=nullptr)
Updates the relevant status bar for the UI represented by object by sending a QStatusTipEvent.
void activate(ActionEvent event)
Sends the relevant signals for ActionEvent event.
QString text
the action's descriptive text
QIcon icon
the action's icon
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.
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.
virtual void setAccepted(bool accepted)
Type type() const
Returns the event type.
void ignore()
Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
void accept()
Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
The QFocusEvent class contains event parameters for widget focus events.
\reentrant \inmodule QtGui
int midLineWidth
the width of the mid-line
int lineWidth
the line width
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...
const QPoint & pos() const
Returns the mouse cursor position when the event was generated, relative to the widget to which the e...
The QIcon class provides scalable icons in different modes and states.
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
The QKeyEvent class describes a key event.
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately after the event occurred.
QString text() const
Returns the Unicode text that this key generated.
int key() const
Returns the code of the key that was pressed or released.
static QKeySequence mnemonic(const QString &text)
Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics ...
qsizetype size() const noexcept
QList< T > & fill(parameter_type t, qsizetype size=-1)
const_reference at(qsizetype i) const noexcept
T value(qsizetype i) const
void reserve(qsizetype size)
void resize(qsizetype size)
void append(parameter_type t)
Qt::MouseEventSource source() const
void installEventFilter(QObject *filterObj)
Installs an event filter filterObj on this object.
QObject * parent() const
Returns a pointer to the parent object.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
void removeEventFilter(QObject *obj)
Removes an event filter object obj from this object.
virtual void timerEvent(QTimerEvent *event)
This event handler can be reimplemented in a subclass to receive timer events for the object.
Q_WEAK_OVERLOAD void setObjectName(const QString &name)
Sets the object's name to name.
The QPaintEvent class contains event parameters for paint events.
const QRect & rect() const
Returns the rectangle that needs to be updated.
The QPainter class performs low-level painting on widgets and other paint devices.
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
bool intersects(const QRect &r) const noexcept
Returns true if this rectangle intersects with the given rectangle (i.e., there is at least one pixel...
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.
constexpr int height() const noexcept
Returns the height of the rectangle.
constexpr bool isNull() const noexcept
Returns true if the rectangle is a null rectangle, otherwise returns false.
constexpr int bottom() const noexcept
Returns the y-coordinate of the rectangle's bottom edge.
constexpr int top() const noexcept
Returns the y-coordinate of the rectangle's top edge.
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...
constexpr int left() const noexcept
Returns the x-coordinate of the rectangle's left edge.
constexpr int x() const noexcept
Returns the x-coordinate of the rectangle's left edge.
constexpr int width() const noexcept
Returns the width of the rectangle.
constexpr int y() const noexcept
Returns the y-coordinate of the rectangle's top edge.
constexpr int right() const noexcept
Returns the x-coordinate of the rectangle's right edge.
The QRegion class specifies a clip region for a painter.
The QResizeEvent class contains event parameters for resize events.
The QScreen class is used to query screen properties. \inmodule QtGui.
QRect virtualGeometry
the pixel geometry of the virtual desktop to which this screen belongs
The QShortcutEvent class provides an event which is generated when the user presses a key combination...
QPointF globalPosition() const
Returns the position of the point in this event on the screen or virtual desktop.
QPointF position() const
Returns the position of the point in this event, relative to the widget or item that received the eve...
Qt::MouseButton button() const
Returns the button that caused the event.
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
constexpr int height() const noexcept
Returns the height.
constexpr int width() const noexcept
Returns the width.
constexpr int & rheight() noexcept
Returns a reference to the height.
constexpr QSize expandedTo(const QSize &) const noexcept
Returns a size holding the maximum width and height of this size and the given otherSize.
constexpr void setWidth(int w) noexcept
Sets the width to the given width.
constexpr bool isEmpty() const noexcept
Returns true if either of the width and height is less than or equal to 0; otherwise returns false.
The QStatusTipEvent class provides an event that is used to show messages in a status bar.
\macro QT_RESTRICTED_CAST_FROM_ASCII
qsizetype indexOf(QLatin1StringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
qsizetype size() const noexcept
Returns the number of characters in this string.
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
\variable QStyleOptionFocusRect::backgroundColor
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.
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
@ SH_MenuBar_AltKeyNavigation
@ SH_Menu_AllowActiveAndDisabled
@ SH_MenuBar_MouseTracking
@ SH_DrawMenuBarSeparator
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
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.
@ PM_ToolBarExtensionExtent
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...
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
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.
Combined button and popup list for selecting options.
@ MouseEventNotSynthesized
@ AA_DontUseNativeMenuBar
static jboolean copy(JNIEnv *, jobject)
AudioChannelLayoutTag tag
constexpr const T & qMax(const T &a, const T &b)
GLboolean GLboolean GLboolean b
GLuint64 GLenum void * handle
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLdouble GLdouble GLdouble GLdouble q
QtConcurrent::task([]{ qDebug("Hello, world!");}).spawn(FutureResult void increment(QPromise< int > &promise, int i)
[10]
qsizetype indexOf(const AT &t, qsizetype from=0) const noexcept
bool contains(const AT &t) const noexcept