diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-02-19 22:36:12 +0100 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-02-20 10:33:10 +0100 |
commit | f99df426466c048c16c3af0a89e7923c0db23818 (patch) | |
tree | 824841e0b9a086f4a400ec89c779dbd73a50fac3 | |
parent | c9df9f3976d969dc1f829a596016a377f1e619f4 (diff) |
mainwindow example: Don't assume toolbar parent is main window
Adding the toolbar to the main window leaves it up to the main window
how to handle ownership and placement of the toolbar.
Pick-to: 6.5
Change-Id: Id429cbad9d5973931a86fcb899684e3651694fbb
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r-- | examples/widgets/mainwindows/mainwindow/toolbar.cpp | 14 | ||||
-rw-r--r-- | examples/widgets/mainwindows/mainwindow/toolbar.h | 4 |
2 files changed, 6 insertions, 12 deletions
diff --git a/examples/widgets/mainwindows/mainwindow/toolbar.cpp b/examples/widgets/mainwindows/mainwindow/toolbar.cpp index 66f26d7a72..d73c12f50d 100644 --- a/examples/widgets/mainwindows/mainwindow/toolbar.cpp +++ b/examples/widgets/mainwindows/mainwindow/toolbar.cpp @@ -37,8 +37,9 @@ static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &col static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color, qreal pixelRatio) { return genIcon(iconSize, QString::number(number), color, pixelRatio); } -ToolBar::ToolBar(const QString &title, QWidget *parent) - : QToolBar(parent) +ToolBar::ToolBar(const QString &title, QMainWindow *mainWindow) + : QToolBar(mainWindow) + , mainWindow(mainWindow) , spinbox(nullptr) , spinboxAction(nullptr) { @@ -161,9 +162,6 @@ ToolBar::ToolBar(const QString &title, QWidget *parent) void ToolBar::updateMenu() { - QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget()); - Q_ASSERT(mainWindow); - const Qt::ToolBarArea area = mainWindow->toolBarArea(this); const Qt::ToolBarAreas areas = allowedAreas(); @@ -267,9 +265,6 @@ void ToolBar::place(Qt::ToolBarArea area, bool p) if (!p) return; - QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget()); - Q_ASSERT(mainWindow); - mainWindow->addToolBar(area, this); if (allowedAreasActions->isEnabled()) { @@ -309,8 +304,5 @@ void ToolBar::placeBottom(bool p) void ToolBar::insertToolBarBreak() { - QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget()); - Q_ASSERT(mainWindow); - mainWindow->insertToolBarBreak(this); } diff --git a/examples/widgets/mainwindows/mainwindow/toolbar.h b/examples/widgets/mainwindows/mainwindow/toolbar.h index 14eca61684..0030adbe23 100644 --- a/examples/widgets/mainwindows/mainwindow/toolbar.h +++ b/examples/widgets/mainwindows/mainwindow/toolbar.h @@ -16,7 +16,7 @@ class ToolBar : public QToolBar Q_OBJECT public: - explicit ToolBar(const QString &title, QWidget *parent); + explicit ToolBar(const QString &title, QMainWindow *mainWindow); QMenu *toolbarMenu() const { return menu; } @@ -45,6 +45,8 @@ private: void allow(Qt::ToolBarArea area, bool allow); void place(Qt::ToolBarArea area, bool place); + QMainWindow *mainWindow; + QSpinBox *spinbox; QAction *spinboxAction; |