Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qmainwindow.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//#define QT_EXPERIMENTAL_CLIENT_DECORATIONS
5
6#include "qmainwindow.h"
8
9#if QT_CONFIG(dockwidget)
10#include "qdockwidget.h"
11#endif
12#if QT_CONFIG(toolbar)
13#include "qtoolbar.h"
14#endif
15
16#include <qapplication.h>
17#include <qmenu.h>
18#if QT_CONFIG(menubar)
19#include <qmenubar.h>
20#endif
21#if QT_CONFIG(statusbar)
22#include <qstatusbar.h>
23#endif
24#include <qevent.h>
25#include <qstyle.h>
26#include <qdebug.h>
27#include <qpainter.h>
28#include <qmimedata.h>
29
30#include <private/qwidget_p.h>
31#if QT_CONFIG(toolbar)
32#include "qtoolbar_p.h"
33#endif
34#include "qwidgetanimator_p.h"
35#include <QtGui/qpa/qplatformwindow.h>
36#include <QtGui/qpa/qplatformwindow_p.h>
37
39
40using namespace Qt::StringLiterals;
41
43{
44 Q_DECLARE_PUBLIC(QMainWindow)
45public:
47 : layout(nullptr), explicitIconSize(false), toolButtonStyle(Qt::ToolButtonIconOnly)
48#ifdef Q_OS_MACOS
49 , useUnifiedToolBar(false)
50#endif
51 { }
56#ifdef Q_OS_MACOS
57 bool useUnifiedToolBar;
58#endif
59 void init();
60
61 static inline QMainWindowLayout *mainWindowLayout(const QMainWindow *mainWindow)
62 {
63 return mainWindow ? mainWindow->d_func()->layout : static_cast<QMainWindowLayout *>(nullptr);
64 }
65};
66
68{
69 return QMainWindowPrivate::mainWindowLayout(mainWindow);
70}
71
72#ifdef QT_EXPERIMENTAL_CLIENT_DECORATIONS
73Q_WIDGETS_EXPORT void qt_setMainWindowTitleWidget(QMainWindow *mainWindow, Qt::DockWidgetArea area, QWidget *widget)
74{
75 QGridLayout *topLayout = qobject_cast<QGridLayout *>(mainWindow->layout());
76 Q_ASSERT(topLayout);
77
78 int row = 0;
79 int column = 0;
80
81 switch (area) {
83 row = 1;
84 column = 0;
85 break;
87 row = 0;
88 column = 1;
89 break;
91 row = 2;
92 column = 1;
93 break;
95 row = 1;
96 column = 2;
97 break;
98 default:
99 Q_ASSERT_X(false, "qt_setMainWindowTitleWidget", "Unknown area");
100 return;
101 }
102
103 if (QLayoutItem *oldItem = topLayout->itemAtPosition(row, column))
104 delete oldItem->widget();
105 topLayout->addWidget(widget, row, column);
106}
107#endif
108
110{
111 Q_Q(QMainWindow);
112
113#ifdef QT_EXPERIMENTAL_CLIENT_DECORATIONS
114 QGridLayout *topLayout = new QGridLayout(q);
115 topLayout->setContentsMargins(0, 0, 0, 0);
116
117 layout = new QMainWindowLayout(q, topLayout);
118
119 topLayout->addItem(layout, 1, 1);
120#else
121 layout = new QMainWindowLayout(q, nullptr);
122#endif
123
124 const int metric = q->style()->pixelMetric(QStyle::PM_ToolBarIconSize, nullptr, q);
125 iconSize = QSize(metric, metric);
126 q->setAttribute(Qt::WA_Hover);
127 q->setAcceptDrops(true);
128}
129
130/*
131 The Main Window:
132
133 +----------------------------------------------------------+
134 | Menu Bar |
135 +----------------------------------------------------------+
136 | Tool Bar Area |
137 | +--------------------------------------------------+ |
138 | | Dock Window Area | |
139 | | +------------------------------------------+ | |
140 | | | | | |
141 | | | Central Widget | | |
142 | | | | | |
143 | | | | | |
144 | | | | | |
145 | | | | | |
146 | | | | | |
147 | | | | | |
148 | | | | | |
149 | | | | | |
150 | | | | | |
151 | | | | | |
152 | | +------------------------------------------+ | |
153 | | | |
154 | +--------------------------------------------------+ |
155 | |
156 +----------------------------------------------------------+
157 | Status Bar |
158 +----------------------------------------------------------+
159
160*/
161
310#if QT_CONFIG(dockwidget)
320#endif
321
331{
332 d_func()->init();
333}
334
335
340{ }
341
398{
399 Q_D(QMainWindow);
400 d->layout->setDockOptions(opt);
401}
402
403QMainWindow::DockOptions QMainWindow::dockOptions() const
404{
405 Q_D(const QMainWindow);
406 return d->layout->dockOptions;
407}
408
410{ return d_func()->iconSize; }
411
413{
414 Q_D(QMainWindow);
415 QSize sz = iconSize;
416 if (!sz.isValid()) {
417 const int metric = style()->pixelMetric(QStyle::PM_ToolBarIconSize, nullptr, this);
418 sz = QSize(metric, metric);
419 }
420 if (d->iconSize != sz) {
421 d->iconSize = sz;
422 emit iconSizeChanged(d->iconSize);
423 }
424 d->explicitIconSize = iconSize.isValid();
425}
426
438{ return d_func()->toolButtonStyle; }
439
441{
442 Q_D(QMainWindow);
443 if (d->toolButtonStyle == toolButtonStyle)
444 return;
445 d->toolButtonStyle = toolButtonStyle;
446 emit toolButtonStyleChanged(d->toolButtonStyle);
447}
448
449#if QT_CONFIG(menubar)
465QMenuBar *QMainWindow::menuBar() const
466{
467 QMenuBar *menuBar = qobject_cast<QMenuBar *>(layout()->menuBar());
468 if (!menuBar) {
469 QMainWindow *self = const_cast<QMainWindow *>(this);
470 menuBar = new QMenuBar(self);
471 self->setMenuBar(menuBar);
472 }
473 return menuBar;
474}
475
484void QMainWindow::setMenuBar(QMenuBar *menuBar)
485{
486 QLayout *topLayout = layout();
487
488 if (QWidget *existingMenuBar = topLayout->menuBar(); existingMenuBar && existingMenuBar != menuBar) {
489 // Reparent corner widgets before we delete the old menu bar.
490 QMenuBar *oldMenuBar = qobject_cast<QMenuBar *>(existingMenuBar);
491 if (oldMenuBar && menuBar) {
492 // TopLeftCorner widget.
493 QWidget *cornerWidget = oldMenuBar->cornerWidget(Qt::TopLeftCorner);
494 if (cornerWidget)
496 // TopRightCorner widget.
497 cornerWidget = oldMenuBar->cornerWidget(Qt::TopRightCorner);
498 if (cornerWidget)
500 }
501
502 existingMenuBar->hide();
503 existingMenuBar->setParent(nullptr);
504 existingMenuBar->deleteLater();
505 }
506 topLayout->setMenuBar(menuBar);
507}
508
515QWidget *QMainWindow::menuWidget() const
516{
517 QWidget *menuBar = d_func()->layout->menuBar();
518 return menuBar;
519}
520
529void QMainWindow::setMenuWidget(QWidget *menuBar)
530{
531 Q_D(QMainWindow);
532 if (d->layout->menuBar() && d->layout->menuBar() != menuBar) {
533 d->layout->menuBar()->hide();
534 d->layout->menuBar()->deleteLater();
535 }
536 d->layout->setMenuBar(menuBar);
537}
538#endif // QT_CONFIG(menubar)
539
540#if QT_CONFIG(statusbar)
547QStatusBar *QMainWindow::statusBar() const
548{
549 QStatusBar *statusbar = d_func()->layout->statusBar();
550 if (!statusbar) {
551 QMainWindow *self = const_cast<QMainWindow *>(this);
552 statusbar = new QStatusBar(self);
554 self->setStatusBar(statusbar);
555 }
556 return statusbar;
557}
558
568void QMainWindow::setStatusBar(QStatusBar *statusbar)
569{
570 Q_D(QMainWindow);
571 if (d->layout->statusBar() && d->layout->statusBar() != statusbar) {
572 d->layout->statusBar()->hide();
573 d->layout->statusBar()->deleteLater();
574 }
575 d->layout->setStatusBar(statusbar);
576}
577#endif // QT_CONFIG(statusbar)
578
586{ return d_func()->layout->centralWidget(); }
587
597{
598 Q_D(QMainWindow);
599 if (d->layout->centralWidget() && d->layout->centralWidget() != widget) {
600 d->layout->centralWidget()->hide();
601 d->layout->centralWidget()->deleteLater();
602 }
603 d->layout->setCentralWidget(widget);
604}
605
614{
615 Q_D(QMainWindow);
616 QWidget *oldcentralwidget = d->layout->centralWidget();
617 if (oldcentralwidget) {
618 oldcentralwidget->setParent(nullptr);
619 d->layout->setCentralWidget(nullptr);
620 }
621 return oldcentralwidget;
622}
623
624#if QT_CONFIG(dockwidget)
631void QMainWindow::setCorner(Qt::Corner corner, Qt::DockWidgetArea area)
632{
633 bool valid = false;
634 switch (corner) {
637 break;
640 break;
643 break;
646 break;
647 }
648 if (Q_UNLIKELY(!valid))
649 qWarning("QMainWindow::setCorner(): 'area' is not valid for 'corner'");
650 else
651 d_func()->layout->setCorner(corner, area);
652}
653
660Qt::DockWidgetArea QMainWindow::corner(Qt::Corner corner) const
661{ return d_func()->layout->corner(corner); }
662#endif
663
664#if QT_CONFIG(toolbar)
665
666static bool checkToolBarArea(Qt::ToolBarArea area, const char *where)
667{
668 switch (area) {
673 return true;
674 default:
675 break;
676 }
677 qWarning("%s: invalid 'area' argument", where);
678 return false;
679}
680
685void QMainWindow::addToolBarBreak(Qt::ToolBarArea area)
686{
687 if (!checkToolBarArea(area, "QMainWindow::addToolBarBreak"))
688 return;
689 d_func()->layout->addToolBarBreak(area);
690}
691
695void QMainWindow::insertToolBarBreak(QToolBar *before)
696{ d_func()->layout->insertToolBarBreak(before); }
697
702void QMainWindow::removeToolBarBreak(QToolBar *before)
703{
704 Q_D(QMainWindow);
705 d->layout->removeToolBarBreak(before);
706}
707
716void QMainWindow::addToolBar(Qt::ToolBarArea area, QToolBar *toolbar)
717{
718 if (!checkToolBarArea(area, "QMainWindow::addToolBar"))
719 return;
720
721 Q_D(QMainWindow);
722
724 toolbar, SLOT(_q_updateIconSize(QSize)));
726 toolbar, SLOT(_q_updateToolButtonStyle(Qt::ToolButtonStyle)));
727
728 if (toolbar->d_func()->state && toolbar->d_func()->state->dragging) {
729 //removing a toolbar which is dragging will cause crash
730#if QT_CONFIG(dockwidget)
731 bool animated = isAnimated();
732 setAnimated(false);
733#endif
734 toolbar->d_func()->endDrag();
735#if QT_CONFIG(dockwidget)
736 setAnimated(animated);
737#endif
738 }
739
740 d->layout->removeToolBar(toolbar);
741
742 toolbar->d_func()->_q_updateIconSize(d->iconSize);
743 toolbar->d_func()->_q_updateToolButtonStyle(d->toolButtonStyle);
745 toolbar, SLOT(_q_updateIconSize(QSize)));
747 toolbar, SLOT(_q_updateToolButtonStyle(Qt::ToolButtonStyle)));
748
749 d->layout->addToolBar(area, toolbar);
750}
751
755void QMainWindow::addToolBar(QToolBar *toolbar)
756{ addToolBar(Qt::TopToolBarArea, toolbar); }
757
766QToolBar *QMainWindow::addToolBar(const QString &title)
767{
768 QToolBar *toolBar = new QToolBar(this);
769 toolBar->setWindowTitle(title);
770 addToolBar(toolBar);
771 return toolBar;
772}
773
782void QMainWindow::insertToolBar(QToolBar *before, QToolBar *toolbar)
783{
784 Q_D(QMainWindow);
785
786 d->layout->removeToolBar(toolbar);
787
788 toolbar->d_func()->_q_updateIconSize(d->iconSize);
789 toolbar->d_func()->_q_updateToolButtonStyle(d->toolButtonStyle);
791 toolbar, SLOT(_q_updateIconSize(QSize)));
793 toolbar, SLOT(_q_updateToolButtonStyle(Qt::ToolButtonStyle)));
794
795 d->layout->insertToolBar(before, toolbar);
796}
797
802void QMainWindow::removeToolBar(QToolBar *toolbar)
803{
804 if (toolbar) {
805 d_func()->layout->removeToolBar(toolbar);
806 toolbar->hide();
807 }
808}
809
817Qt::ToolBarArea QMainWindow::toolBarArea(const QToolBar *toolbar) const
818{ return d_func()->layout->toolBarArea(toolbar); }
819
827bool QMainWindow::toolBarBreak(QToolBar *toolbar) const
828{
829 return d_func()->layout->toolBarBreak(toolbar);
830}
831
832#endif // QT_CONFIG(toolbar)
833
834#if QT_CONFIG(dockwidget)
835
856bool QMainWindow::isAnimated() const
857{
858 Q_D(const QMainWindow);
859 return d->layout->dockOptions & AnimatedDocks;
860}
861
862void QMainWindow::setAnimated(bool enabled)
863{
864 Q_D(QMainWindow);
865
866 DockOptions opts = d->layout->dockOptions;
867 opts.setFlag(AnimatedDocks, enabled);
868
869 d->layout->setDockOptions(opts);
870}
871
892bool QMainWindow::isDockNestingEnabled() const
893{
894 Q_D(const QMainWindow);
895 return d->layout->dockOptions & AllowNestedDocks;
896}
897
898void QMainWindow::setDockNestingEnabled(bool enabled)
899{
900 Q_D(QMainWindow);
901
902 DockOptions opts = d->layout->dockOptions;
903 opts.setFlag(AllowNestedDocks, enabled);
904
905 d->layout->setDockOptions(opts);
906}
907
908#if 0
909// If added back in, add the '!' to the qdoc comment marker as well.
910/*
911 \property QMainWindow::verticalTabsEnabled
912 \brief whether left and right dock areas use vertical tabs
913 \since 4.2
914
915 If this property is set to false, dock areas containing tabbed dock widgets
916 display horizontal tabs, similar to Visual Studio.
917
918 If this property is set to true, then the right and left dock areas display vertical
919 tabs, similar to KDevelop.
920
921 This property should be set before any dock widgets are added to the main window.
922*/
923
924bool QMainWindow::verticalTabsEnabled() const
925{
926 return d_func()->layout->verticalTabsEnabled();
927}
928
929void QMainWindow::setVerticalTabsEnabled(bool enabled)
930{
931 d_func()->layout->setVerticalTabsEnabled(enabled);
932}
933#endif
934
935static bool checkDockWidgetArea(Qt::DockWidgetArea area, const char *where)
936{
937 switch (area) {
942 return true;
943 default:
944 break;
945 }
946 qWarning("%s: invalid 'area' argument", where);
947 return false;
948}
949
950#if QT_CONFIG(tabbar)
960bool QMainWindow::documentMode() const
961{
962 return d_func()->layout->documentMode();
963}
964
965void QMainWindow::setDocumentMode(bool enabled)
966{
967 d_func()->layout->setDocumentMode(enabled);
968}
969#endif // QT_CONFIG(tabbar)
970
971#if QT_CONFIG(tabwidget)
981QTabWidget::TabShape QMainWindow::tabShape() const
982{
983 return d_func()->layout->tabShape();
984}
985
986void QMainWindow::setTabShape(QTabWidget::TabShape tabShape)
987{
988 d_func()->layout->setTabShape(tabShape);
989}
990
1001QTabWidget::TabPosition QMainWindow::tabPosition(Qt::DockWidgetArea area) const
1002{
1003 if (!checkDockWidgetArea(area, "QMainWindow::tabPosition"))
1004 return QTabWidget::South;
1005 return d_func()->layout->tabPosition(area);
1006}
1007
1019void QMainWindow::setTabPosition(Qt::DockWidgetAreas areas, QTabWidget::TabPosition tabPosition)
1020{
1021 d_func()->layout->setTabPosition(areas, tabPosition);
1022}
1023#endif // QT_CONFIG(tabwidget)
1024
1028void QMainWindow::addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockwidget)
1029{
1030 if (!checkDockWidgetArea(area, "QMainWindow::addDockWidget"))
1031 return;
1032
1033 Qt::Orientation orientation = Qt::Vertical;
1034 switch (area) {
1037 orientation = Qt::Horizontal;
1038 break;
1039 default:
1040 break;
1041 }
1042 d_func()->layout->removeWidget(dockwidget); // in case it was already in here
1043 addDockWidget(area, dockwidget, orientation);
1044}
1045
1054bool QMainWindow::restoreDockWidget(QDockWidget *dockwidget)
1055{
1056 return d_func()->layout->restoreDockWidget(dockwidget);
1057}
1058
1063void QMainWindow::addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockwidget,
1064 Qt::Orientation orientation)
1065{
1066 if (!checkDockWidgetArea(area, "QMainWindow::addDockWidget"))
1067 return;
1068
1069 // add a window to an area, placing done relative to the previous
1070 d_func()->layout->addDockWidget(area, dockwidget, orientation);
1071}
1072
1094void QMainWindow::splitDockWidget(QDockWidget *after, QDockWidget *dockwidget,
1095 Qt::Orientation orientation)
1096{
1097 d_func()->layout->splitDockWidget(after, dockwidget, orientation);
1098}
1099
1100#if QT_CONFIG(tabbar)
1109void QMainWindow::tabifyDockWidget(QDockWidget *first, QDockWidget *second)
1110{
1111 d_func()->layout->tabifyDockWidget(first, second);
1112}
1113
1114
1124QList<QDockWidget*> QMainWindow::tabifiedDockWidgets(QDockWidget *dockwidget) const
1125{
1127 const QDockAreaLayoutInfo *info = d_func()->layout->layoutState.dockAreaLayout.info(dockwidget);
1128 if (info && info->tabbed && info->tabBar) {
1129 for(int i = 0; i < info->item_list.size(); ++i) {
1130 const QDockAreaLayoutItem &item = info->item_list.at(i);
1131 if (item.widgetItem) {
1132 if (QDockWidget *dock = qobject_cast<QDockWidget*>(item.widgetItem->widget())) {
1133 if (dock != dockwidget) {
1134 ret += dock;
1135 }
1136 }
1137 }
1138 }
1139 }
1140 return ret;
1141}
1142#endif // QT_CONFIG(tabbar)
1143
1144
1149void QMainWindow::removeDockWidget(QDockWidget *dockwidget)
1150{
1151 if (dockwidget) {
1152 d_func()->layout->removeWidget(dockwidget);
1153 dockwidget->hide();
1154 }
1155}
1156
1164Qt::DockWidgetArea QMainWindow::dockWidgetArea(QDockWidget *dockwidget) const
1165{ return d_func()->layout->dockWidgetArea(dockwidget); }
1166
1167
1187void QMainWindow::resizeDocks(const QList<QDockWidget *> &docks,
1188 const QList<int> &sizes, Qt::Orientation orientation)
1189{
1190 d_func()->layout->layoutState.dockAreaLayout.resizeDocks(docks, sizes, orientation);
1191 d_func()->layout->invalidate();
1192}
1193
1194
1195#endif // QT_CONFIG(dockwidget)
1196
1219{
1222 stream.setVersion(QDataStream::Qt_5_0);
1224 stream << version;
1225 d_func()->layout->saveState(stream);
1226 return data;
1227}
1228
1246{
1247 if (state.isEmpty())
1248 return false;
1249 QByteArray sd = state;
1251 stream.setVersion(QDataStream::Qt_5_0);
1252 int marker, v;
1253 stream >> marker;
1254 stream >> v;
1255 if (stream.status() != QDataStream::Ok || marker != QMainWindowLayout::VersionMarker || v != version)
1256 return false;
1257 bool restored = d_func()->layout->restoreState(stream);
1258 return restored;
1259}
1260
1263{
1264 Q_D(QMainWindow);
1265
1266#if QT_CONFIG(dockwidget)
1267 if (d->layout && d->layout->windowEvent(event))
1268 return true;
1269#endif
1270
1271 switch (event->type()) {
1272
1273#if QT_CONFIG(toolbar)
1274 case QEvent::ToolBarChange: {
1275 Q_ASSERT(d->layout);
1276 d->layout->toggleToolBarsVisible();
1277 return true;
1278 }
1279#endif
1280
1281#if QT_CONFIG(statustip)
1282 case QEvent::StatusTip:
1283#if QT_CONFIG(statusbar)
1284 Q_ASSERT(d->layout);
1285 if (QStatusBar *sb = d->layout->statusBar())
1286 sb->showMessage(static_cast<QStatusTipEvent*>(event)->tip());
1287 else
1288#endif
1289 static_cast<QStatusTipEvent*>(event)->ignore();
1290 return true;
1291#endif // QT_CONFIG(statustip)
1292
1294#if QT_CONFIG(dockwidget)
1295 Q_ASSERT(d->layout);
1296 d->layout->layoutState.dockAreaLayout.styleChangedEvent();
1297#endif
1298 if (!d->explicitIconSize)
1299 setIconSize(QSize());
1300 break;
1301#if QT_CONFIG(draganddrop)
1302 case QEvent::DragEnter:
1303 case QEvent::Drop:
1304 if (!d->layout->draggingWidget)
1305 break;
1306 event->accept();
1307 return true;
1308 case QEvent::DragMove: {
1309 if (!d->layout->draggingWidget)
1310 break;
1311 auto dragMoveEvent = static_cast<QDragMoveEvent *>(event);
1312 d->layout->hover(d->layout->draggingWidget, dragMoveEvent->position().toPoint());
1313 event->accept();
1314 return true;
1315 }
1316#endif
1317 default:
1318 break;
1319 }
1320
1321 return QWidget::event(event);
1322}
1323
1324#if QT_CONFIG(toolbar)
1325
1338void QMainWindow::setUnifiedTitleAndToolBarOnMac(bool enabled)
1339{
1340#ifdef Q_OS_MACOS
1341 if (!isWindow())
1342 return;
1343
1344 Q_D(QMainWindow);
1345 d->useUnifiedToolBar = enabled;
1346
1347 // The unified toolbar is drawn by the macOS style with a transparent background.
1348 // To ensure a suitable surface format is used we need to first create backing
1349 // QWindow so we have something to update the surface format on, and then let
1350 // QWidget know about the translucency, which it will propagate to the surface.
1353
1354 d->create(); // Create first, before querying the platform window
1355 using namespace QNativeInterface::Private;
1356 if (auto *platformWindow = dynamic_cast<QCocoaWindow*>(window()->windowHandle()->handle()))
1357 platformWindow->setContentBorderEnabled(enabled);
1358
1359 update();
1360#else
1362#endif
1363}
1364
1365bool QMainWindow::unifiedTitleAndToolBarOnMac() const
1366{
1367#ifdef Q_OS_MACOS
1368 return d_func()->useUnifiedToolBar;
1369#endif
1370 return false;
1371}
1372
1373#endif // QT_CONFIG(toolbar)
1374
1379{
1380#if QT_CONFIG(dockwidget)
1381 Q_D(const QMainWindow);
1382 return !d->layout->layoutState.dockAreaLayout.findSeparator(pos).isEmpty();
1383#else
1384 Q_UNUSED(pos);
1385 return false;
1386#endif
1387}
1388
1389#ifndef QT_NO_CONTEXTMENU
1394{
1395 event->ignore();
1396 // only show the context menu for direct QDockWidget and QToolBar
1397 // children and for the menu bar as well
1398 QWidget *child = childAt(event->pos());
1399 while (child && child != this) {
1400#if QT_CONFIG(menubar)
1401 if (QMenuBar *mb = qobject_cast<QMenuBar *>(child)) {
1402 if (mb->parentWidget() != this)
1403 return;
1404 break;
1405 }
1406#endif
1407#if QT_CONFIG(dockwidget)
1408 if (QDockWidget *dw = qobject_cast<QDockWidget *>(child)) {
1409 if (dw->parentWidget() != this)
1410 return;
1411 if (dw->widget()
1412 && dw->widget()->geometry().contains(child->mapFrom(this, event->pos()))) {
1413 // ignore the event if the mouse is over the QDockWidget contents
1414 return;
1415 }
1416 break;
1417 }
1418#endif // QT_CONFIG(dockwidget)
1419#if QT_CONFIG(toolbar)
1420 if (QToolBar *tb = qobject_cast<QToolBar *>(child)) {
1421 if (tb->parentWidget() != this)
1422 return;
1423 break;
1424 }
1425#endif
1426 child = child->parentWidget();
1427 }
1428 if (child == this)
1429 return;
1430
1431#if QT_CONFIG(menu)
1432 QMenu *popup = createPopupMenu();
1433 if (popup) {
1434 if (!popup->isEmpty()) {
1436 popup->popup(event->globalPos());
1437 event->accept();
1438 } else {
1439 delete popup;
1440 }
1441 }
1442#endif
1443}
1444#endif // QT_NO_CONTEXTMENU
1445
1446#if QT_CONFIG(menu)
1462QMenu *QMainWindow::createPopupMenu()
1463{
1464 Q_D(QMainWindow);
1465 QMenu *menu = nullptr;
1466#if QT_CONFIG(dockwidget)
1467 QList<QDockWidget *> dockwidgets = findChildren<QDockWidget *>();
1468 if (dockwidgets.size()) {
1469 menu = new QMenu(this);
1470 for (int i = 0; i < dockwidgets.size(); ++i) {
1471 QDockWidget *dockWidget = dockwidgets.at(i);
1472 // filter to find out if we own this QDockWidget
1473 if (dockWidget->parentWidget() == this) {
1474 if (d->layout->layoutState.dockAreaLayout.indexOf(dockWidget).isEmpty())
1475 continue;
1476 } else if (QDockWidgetGroupWindow *dwgw =
1477 qobject_cast<QDockWidgetGroupWindow *>(dockWidget->parentWidget())) {
1478 if (dwgw->parentWidget() != this)
1479 continue;
1480 if (dwgw->layoutInfo()->indexOf(dockWidget).isEmpty())
1481 continue;
1482 } else {
1483 continue;
1484 }
1485 menu->addAction(dockwidgets.at(i)->toggleViewAction());
1486 }
1487 menu->addSeparator();
1488 }
1489#endif // QT_CONFIG(dockwidget)
1490#if QT_CONFIG(toolbar)
1491 QList<QToolBar *> toolbars = findChildren<QToolBar *>();
1492 if (toolbars.size()) {
1493 if (!menu)
1494 menu = new QMenu(this);
1495 for (int i = 0; i < toolbars.size(); ++i) {
1496 QToolBar *toolBar = toolbars.at(i);
1497 if (toolBar->parentWidget() == this
1498 && (!d->layout->layoutState.toolBarAreaLayout.indexOf(toolBar).isEmpty())) {
1499 menu->addAction(toolbars.at(i)->toggleViewAction());
1500 }
1501 }
1502 }
1503#endif
1504 Q_UNUSED(d);
1505 return menu;
1506}
1507#endif // QT_CONFIG(menu)
1508
1510
1511#include "moc_qmainwindow.cpp"
\inmodule QtCore
Definition qbytearray.h:57
The QContextMenuEvent class contains parameters that describe a context menu event.
Definition qevent.h:593
\inmodule QtCore\reentrant
Definition qdatastream.h:30
The QDockWidget class provides a widget that can be docked inside a QMainWindow or floated as a top-l...
Definition qdockwidget.h:20
\inmodule QtCore
Definition qcoreevent.h:45
@ StatusTip
Definition qcoreevent.h:149
@ StyleChange
Definition qcoreevent.h:136
@ DragEnter
Definition qcoreevent.h:101
@ ToolBarChange
Definition qcoreevent.h:162
qint64 size() const
Returns the file size in bytes.
The QGridLayout class lays out widgets in a grid.
Definition qgridlayout.h:21
void addWidget(QWidget *w)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qgridlayout.h:64
QLayoutItem * itemAtPosition(int row, int column) const
void addItem(QLayoutItem *item, int row, int column, int rowSpan=1, int columnSpan=1, Qt::Alignment=Qt::Alignment())
Adds item at position row, column, spanning rowSpan rows and columnSpan columns, and aligns it accord...
The QLayoutItem class provides an abstract item that a QLayout manipulates.
Definition qlayoutitem.h:25
The QLayout class is the base class of geometry managers.
Definition qlayout.h:26
QWidget * menuBar() const
Returns the menu bar set for this layout, or \nullptr if no menu bar is set.
Definition qlayout.cpp:894
void setMenuBar(QWidget *w)
Tells the geometry manager to place the menu bar widget at the top of parentWidget(),...
Definition qlayout.cpp:881
void setContentsMargins(int left, int top, int right, int bottom)
Definition qlayout.cpp:288
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
Qt::ToolButtonStyle toolButtonStyle
QMainWindowLayout * layout
static QMainWindowLayout * mainWindowLayout(const QMainWindow *mainWindow)
The QMainWindow class provides a main application window.
Definition qmainwindow.h:25
void iconSizeChanged(const QSize &iconSize)
This signal is emitted when the size of the icons used in the window is changed.
QByteArray saveState(int version=0) const
Saves the current state of this mainwindow's toolbars and dockwidgets.
DockOptions dockOptions
the docking behavior of QMainWindow
Definition qmainwindow.h:40
void contextMenuEvent(QContextMenuEvent *event) override
\reimp
bool event(QEvent *event) override
\reimp
void toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)
This signal is emitted when the style used for tool buttons in the window is changed.
QSize iconSize
size of toolbar icons in this mainwindow.
Definition qmainwindow.h:28
QWidget * takeCentralWidget()
Removes the central widget from this main window.
void setIconSize(const QSize &iconSize)
QWidget * centralWidget() const
Returns the central widget for the main window.
QMainWindow(QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructs a QMainWindow with the given parent and the specified widget flags.
Qt::ToolButtonStyle toolButtonStyle
style of toolbar buttons in this mainwindow.
Definition qmainwindow.h:29
bool isSeparator(const QPoint &pos) const
friend class QDockWidgetGroupWindow
void setCentralWidget(QWidget *widget)
Sets the given widget to be the main window's central widget.
void setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)
void setDockOptions(DockOptions options)
bool restoreState(const QByteArray &state, int version=0)
Restores the state of this mainwindow's toolbars and dockwidgets.
~QMainWindow()
Destroys the main window.
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 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,...
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
bool isEmpty() const
Definition qmenu.cpp:2185
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 * addSeparator()
This convenience function creates a new separator action, i.e.
Definition qmenu.cpp:1899
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3124
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
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore
Definition qsize.h:25
constexpr bool isValid() const noexcept
Returns true if both the width and height is equal to or greater than 0; otherwise returns false.
Definition qsize.h:126
The QStatusBar class provides a horizontal bar suitable for presenting status information.
Definition qstatusbar.h:17
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
@ PM_ToolBarIconSize
Definition qstyle.h:490
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const =0
Returns the value of the given pixel metric.
TabPosition
This enum type defines where QTabWidget draws the tab row:
Definition qtabwidget.h:74
TabShape
This enum type defines the shape of the tabs: \value Rounded The tabs are drawn with a rounded look.
Definition qtabwidget.h:85
The QToolBar class provides a movable panel that contains a set of controls.
Definition qtoolbar.h:23
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
int metric(PaintDeviceMetric) const override
Internal implementation of the virtual QPaintDevice::metric() function.
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 setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
void setSizePolicy(QSizePolicy)
QLayout * layout() const
Returns the layout manager that is installed on this widget, or \nullptr if no layout manager is inst...
QWidget * childAt(int x, int y) const
Returns the visible child widget at the position ({x}, {y}) in the widget's coordinate system.
Definition qwidget.h:798
QPoint pos
the position of the widget within its parent widget
Definition qwidget.h:111
void hide()
Hides the widget.
Definition qwidget.cpp:8209
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
bool enabled
whether the widget is enabled
Definition qwidget.h:105
void setWindowTitle(const QString &)
Definition qwidget.cpp:6109
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
QWidget * parentWidget() const
Returns the parent of this widget, or \nullptr if it does not have any parent widget.
Definition qwidget.h:904
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition qwidget.h:811
QOpenGLWidget * widget
[1]
opt iconSize
QStyleOptionButton opt
else opt state
[0]
Combined button and popup list for selecting options.
@ BottomLeftCorner
@ TopRightCorner
@ TopLeftCorner
@ BottomRightCorner
DockWidgetArea
@ BottomDockWidgetArea
@ RightDockWidgetArea
@ LeftDockWidgetArea
@ TopDockWidgetArea
@ WA_Hover
Definition qnamespace.h:339
@ WA_TranslucentBackground
Definition qnamespace.h:401
@ WA_NativeWindow
Definition qnamespace.h:377
@ WA_DeleteOnClose
Definition qnamespace.h:320
ToolBarArea
@ LeftToolBarArea
@ BottomToolBarArea
@ TopToolBarArea
@ RightToolBarArea
Orientation
Definition qnamespace.h:97
@ Horizontal
Definition qnamespace.h:98
@ Vertical
Definition qnamespace.h:99
ToolButtonStyle
QString self
Definition language.cpp:57
#define Q_UNLIKELY(x)
EGLStreamKHR stream
static int area(const QSize &s)
Definition qicon.cpp:152
#define qWarning
Definition qlogging.h:162
return ret
QMainWindowLayout * qt_mainwindow_layout(const QMainWindow *mainWindow)
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLsizei const GLfloat * v
[13]
GLuint64 GLenum void * handle
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
const GLchar * marker
GLint first
GLenum GLenum GLsizei void GLsizei void * column
struct _cl_event * event
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
#define emit
#define Q_UNUSED(x)
XID Window
QFileInfo info(fileName)
[8]
QObject::connect nullptr
QString title
[35]
myObject disconnect()
[26]
QGraphicsItem * item
QLayoutItem * child
[0]
QDockWidget * dockWidget
[0]
QMenu menu
[5]
QMenuBar * menuBar
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent