Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 { }
52 QPointer<QMainWindowLayout> layout;
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.data() : static_cast<QMainWindowLayout *>(nullptr);
64 }
65};
66
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
329QMainWindow::QMainWindow(QWidget *parent, Qt::WindowFlags flags)
330 : QWidget(*(new QMainWindowPrivate()), parent, flags | Qt::Window)
331{
332 d_func()->init();
333}
334
335
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{
1126 Q_D(const QMainWindow);
1127 return d->layout ? d->layout->tabifiedDockWidgets(dockwidget) : QList<QDockWidget *>();
1128}
1129#endif // QT_CONFIG(tabbar)
1130
1131
1136void QMainWindow::removeDockWidget(QDockWidget *dockwidget)
1137{
1138 if (dockwidget) {
1139 d_func()->layout->removeWidget(dockwidget);
1140 dockwidget->hide();
1141 }
1142}
1143
1151Qt::DockWidgetArea QMainWindow::dockWidgetArea(QDockWidget *dockwidget) const
1152{ return d_func()->layout->dockWidgetArea(dockwidget); }
1153
1154
1174void QMainWindow::resizeDocks(const QList<QDockWidget *> &docks,
1175 const QList<int> &sizes, Qt::Orientation orientation)
1176{
1177 d_func()->layout->layoutState.dockAreaLayout.resizeDocks(docks, sizes, orientation);
1178 d_func()->layout->invalidate();
1179}
1180
1181
1182#endif // QT_CONFIG(dockwidget)
1183
1206{
1209 stream.setVersion(QDataStream::Qt_5_0);
1211 stream << version;
1212 d_func()->layout->saveState(stream);
1213 return data;
1214}
1215
1233{
1234 if (state.isEmpty())
1235 return false;
1236 QByteArray sd = state;
1238 stream.setVersion(QDataStream::Qt_5_0);
1239 int marker, v;
1240 stream >> marker;
1241 stream >> v;
1242 if (stream.status() != QDataStream::Ok || marker != QMainWindowLayout::VersionMarker || v != version)
1243 return false;
1244 bool restored = d_func()->layout->restoreState(stream);
1245 return restored;
1246}
1247
1250{
1251 Q_D(QMainWindow);
1252
1253#if QT_CONFIG(dockwidget)
1254 if (d->layout && d->layout->windowEvent(event))
1255 return true;
1256#endif
1257
1258 switch (event->type()) {
1259
1260#if QT_CONFIG(toolbar)
1261 case QEvent::ToolBarChange: {
1262 Q_ASSERT(d->layout);
1263 d->layout->toggleToolBarsVisible();
1264 return true;
1265 }
1266#endif
1267
1268#if QT_CONFIG(statustip)
1269 case QEvent::StatusTip:
1270#if QT_CONFIG(statusbar)
1271 Q_ASSERT(d->layout);
1272 if (QStatusBar *sb = d->layout->statusBar())
1273 sb->showMessage(static_cast<QStatusTipEvent*>(event)->tip());
1274 else
1275#endif
1276 static_cast<QStatusTipEvent*>(event)->ignore();
1277 return true;
1278#endif // QT_CONFIG(statustip)
1279
1281#if QT_CONFIG(dockwidget)
1282 Q_ASSERT(d->layout);
1283 d->layout->layoutState.dockAreaLayout.styleChangedEvent();
1284#endif
1285 if (!d->explicitIconSize)
1286 setIconSize(QSize());
1287 break;
1288#if QT_CONFIG(draganddrop)
1289 case QEvent::DragEnter:
1290 case QEvent::Drop:
1291 if (!d->layout->draggingWidget)
1292 break;
1293 event->accept();
1294 return true;
1295 case QEvent::DragMove: {
1296 if (!d->layout->draggingWidget)
1297 break;
1298 auto dragMoveEvent = static_cast<QDragMoveEvent *>(event);
1299 d->layout->hover(d->layout->draggingWidget,
1300 mapToGlobal(dragMoveEvent->position()).toPoint());
1301 event->accept();
1302 return true;
1303 }
1304 case QEvent::DragLeave:
1305 if (!d->layout->draggingWidget)
1306 break;
1307 d->layout->hover(d->layout->draggingWidget, pos() - QPoint(-1, -1));
1308 return true;
1309#endif
1310 default:
1311 break;
1312 }
1313
1314 return QWidget::event(event);
1315}
1316
1317#if QT_CONFIG(toolbar)
1318
1331void QMainWindow::setUnifiedTitleAndToolBarOnMac(bool enabled)
1332{
1333#ifdef Q_OS_MACOS
1334 if (!isWindow())
1335 return;
1336
1337 Q_D(QMainWindow);
1338 d->useUnifiedToolBar = enabled;
1339
1340 // The unified toolbar is drawn by the macOS style with a transparent background.
1341 // To ensure a suitable surface format is used we need to first create backing
1342 // QWindow so we have something to update the surface format on, and then let
1343 // QWidget know about the translucency, which it will propagate to the surface.
1346
1347 d->create(); // Create first, before querying the platform window
1348 using namespace QNativeInterface::Private;
1349 if (auto *platformWindow = dynamic_cast<QCocoaWindow*>(window()->windowHandle()->handle()))
1350 platformWindow->setContentBorderEnabled(enabled);
1351
1352 update();
1353#else
1355#endif
1356}
1357
1358bool QMainWindow::unifiedTitleAndToolBarOnMac() const
1359{
1360#ifdef Q_OS_MACOS
1361 return d_func()->useUnifiedToolBar;
1362#endif
1363 return false;
1364}
1365
1366#endif // QT_CONFIG(toolbar)
1367
1372{
1373#if QT_CONFIG(dockwidget)
1374 Q_D(const QMainWindow);
1375 return !d->layout->layoutState.dockAreaLayout.findSeparator(pos).isEmpty();
1376#else
1377 Q_UNUSED(pos);
1378 return false;
1379#endif
1380}
1381
1382#ifndef QT_NO_CONTEXTMENU
1387{
1388 event->ignore();
1389 // only show the context menu for direct QDockWidget and QToolBar
1390 // children and for the menu bar as well
1391 QWidget *child = childAt(event->pos());
1392 while (child && child != this) {
1393#if QT_CONFIG(menubar)
1394 if (QMenuBar *mb = qobject_cast<QMenuBar *>(child)) {
1395 if (mb->parentWidget() != this)
1396 return;
1397 break;
1398 }
1399#endif
1400#if QT_CONFIG(dockwidget)
1401 if (QDockWidget *dw = qobject_cast<QDockWidget *>(child)) {
1402 if (dw->parentWidget() != this)
1403 return;
1404 if (dw->widget()
1405 && dw->widget()->geometry().contains(child->mapFrom(this, event->pos()))) {
1406 // ignore the event if the mouse is over the QDockWidget contents
1407 return;
1408 }
1409 break;
1410 }
1411#endif // QT_CONFIG(dockwidget)
1412#if QT_CONFIG(toolbar)
1413 if (QToolBar *tb = qobject_cast<QToolBar *>(child)) {
1414 if (tb->parentWidget() != this)
1415 return;
1416 break;
1417 }
1418#endif
1419 child = child->parentWidget();
1420 }
1421 if (child == this)
1422 return;
1423
1424#if QT_CONFIG(menu)
1425 QMenu *popup = createPopupMenu();
1426 if (popup) {
1427 if (!popup->isEmpty()) {
1429 popup->popup(event->globalPos());
1430 event->accept();
1431 } else {
1432 delete popup;
1433 }
1434 }
1435#endif
1436}
1437#endif // QT_NO_CONTEXTMENU
1438
1439#if QT_CONFIG(menu)
1455QMenu *QMainWindow::createPopupMenu()
1456{
1457 Q_D(QMainWindow);
1458 QMenu *menu = nullptr;
1459#if QT_CONFIG(dockwidget)
1460 QList<QDockWidget *> dockwidgets = findChildren<QDockWidget *>();
1461 if (dockwidgets.size()) {
1462 menu = new QMenu(this);
1463 for (int i = 0; i < dockwidgets.size(); ++i) {
1464 QDockWidget *dockWidget = dockwidgets.at(i);
1465 // filter to find out if we own this QDockWidget
1466 if (dockWidget->parentWidget() == this) {
1467 if (d->layout->layoutState.dockAreaLayout.indexOf(dockWidget).isEmpty())
1468 continue;
1469 } else if (QDockWidgetGroupWindow *dwgw =
1470 qobject_cast<QDockWidgetGroupWindow *>(dockWidget->parentWidget())) {
1471 if (dwgw->parentWidget() != this)
1472 continue;
1473 if (dwgw->layoutInfo()->indexOf(dockWidget).isEmpty())
1474 continue;
1475 } else {
1476 continue;
1477 }
1478 menu->addAction(dockwidgets.at(i)->toggleViewAction());
1479 }
1480 menu->addSeparator();
1481 }
1482#endif // QT_CONFIG(dockwidget)
1483#if QT_CONFIG(toolbar)
1484 QList<QToolBar *> toolbars = findChildren<QToolBar *>();
1485 if (toolbars.size()) {
1486 if (!menu)
1487 menu = new QMenu(this);
1488 for (int i = 0; i < toolbars.size(); ++i) {
1489 QToolBar *toolBar = toolbars.at(i);
1490 if (toolBar->parentWidget() == this
1491 && (!d->layout->layoutState.toolBarAreaLayout.indexOf(toolBar).isEmpty())) {
1492 menu->addAction(toolbars.at(i)->toggleViewAction());
1493 }
1494 }
1495 }
1496#endif
1497 Q_UNUSED(d);
1498 return menu;
1499}
1500#endif // QT_CONFIG(menu)
1501
1503
1504#include "moc_qmainwindow.cpp"
\inmodule QtCore
Definition qbytearray.h:57
The QContextMenuEvent class contains parameters that describe a context menu event.
Definition qevent.h:594
\inmodule QtCore\reentrant
Definition qdatastream.h:46
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
@ DragLeave
Definition qcoreevent.h:103
The QGridLayout class lays out widgets in a grid.
Definition qgridlayout.h:21
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:895
Qt::ToolButtonStyle toolButtonStyle
static QMainWindowLayout * mainWindowLayout(const QMainWindow *mainWindow)
QPointer< QMainWindowLayout > layout
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
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:2207
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:2310
QAction * addSeparator()
This convenience function creates a new separator action, i.e.
Definition qmenu.cpp:1921
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3117
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void setParent(QObject *parent)
Makes the object a child of parent.
Definition qobject.cpp:2195
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition qpoint.h:404
\inmodule QtCore\reentrant
Definition qpoint.h:25
\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:127
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:808
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
@ PM_ToolBarIconSize
Definition qstyle.h:492
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:4313
void setSizePolicy(QSizePolicy)
QPointF mapToGlobal(const QPointF &) const
Translates the widget coordinate pos to global screen coordinates.
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:8135
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:2483
bool enabled
whether the widget is enabled
Definition qwidget.h:105
bool event(QEvent *event) override
This is the main event handler; it handles event event.
Definition qwidget.cpp:8866
QStyle * style() const
Definition qwidget.cpp:2600
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.
Definition qcompare.h:63
@ BottomLeftCorner
@ TopRightCorner
@ TopLeftCorner
@ BottomRightCorner
DockWidgetArea
@ BottomDockWidgetArea
@ RightDockWidgetArea
@ LeftDockWidgetArea
@ TopDockWidgetArea
@ WA_Hover
Definition qnamespace.h:340
@ WA_TranslucentBackground
Definition qnamespace.h:402
@ WA_NativeWindow
Definition qnamespace.h:378
@ WA_DeleteOnClose
Definition qnamespace.h:321
ToolBarArea
@ LeftToolBarArea
@ BottomToolBarArea
@ TopToolBarArea
@ RightToolBarArea
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
ToolButtonStyle
QString self
Definition language.cpp:58
#define Q_UNLIKELY(x)
EGLStreamKHR stream
static int area(const QSize &s)
Definition qicon.cpp:153
#define qWarning
Definition qlogging.h:166
QMainWindowLayout * qt_mainwindow_layout(const QMainWindow *mainWindow)
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLsizei const GLfloat * v
[13]
GLuint64 GLenum void * handle
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLbitfield flags
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)
QObject::connect nullptr
QString title
[35]
myObject disconnect()
[26]
QLayoutItem * child
[0]
QDockWidget * dockWidget
[0]
QMenu menu
[5]
QMenuBar * menuBar
[0]