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.

Change-Id: Id429cbad9d5973931a86fcb899684e3651694fbb
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit f99df426466c048c16c3af0a89e7923c0db23818)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2023-02-19 22:36:12 +01:00 committed by Qt Cherry-pick Bot
parent abb25d681f
commit 4c6882d30c
2 changed files with 6 additions and 12 deletions

View File

@ -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) static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color, qreal pixelRatio)
{ return genIcon(iconSize, QString::number(number), color, pixelRatio); } { return genIcon(iconSize, QString::number(number), color, pixelRatio); }
ToolBar::ToolBar(const QString &title, QWidget *parent) ToolBar::ToolBar(const QString &title, QMainWindow *mainWindow)
: QToolBar(parent) : QToolBar(mainWindow)
, mainWindow(mainWindow)
, spinbox(nullptr) , spinbox(nullptr)
, spinboxAction(nullptr) , spinboxAction(nullptr)
{ {
@ -161,9 +162,6 @@ ToolBar::ToolBar(const QString &title, QWidget *parent)
void ToolBar::updateMenu() void ToolBar::updateMenu()
{ {
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
Q_ASSERT(mainWindow);
const Qt::ToolBarArea area = mainWindow->toolBarArea(this); const Qt::ToolBarArea area = mainWindow->toolBarArea(this);
const Qt::ToolBarAreas areas = allowedAreas(); const Qt::ToolBarAreas areas = allowedAreas();
@ -267,9 +265,6 @@ void ToolBar::place(Qt::ToolBarArea area, bool p)
if (!p) if (!p)
return; return;
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
Q_ASSERT(mainWindow);
mainWindow->addToolBar(area, this); mainWindow->addToolBar(area, this);
if (allowedAreasActions->isEnabled()) { if (allowedAreasActions->isEnabled()) {
@ -309,8 +304,5 @@ void ToolBar::placeBottom(bool p)
void ToolBar::insertToolBarBreak() void ToolBar::insertToolBarBreak()
{ {
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
Q_ASSERT(mainWindow);
mainWindow->insertToolBarBreak(this); mainWindow->insertToolBarBreak(this);
} }

View File

@ -16,7 +16,7 @@ class ToolBar : public QToolBar
Q_OBJECT Q_OBJECT
public: public:
explicit ToolBar(const QString &title, QWidget *parent); explicit ToolBar(const QString &title, QMainWindow *mainWindow);
QMenu *toolbarMenu() const { return menu; } QMenu *toolbarMenu() const { return menu; }
@ -45,6 +45,8 @@ private:
void allow(Qt::ToolBarArea area, bool allow); void allow(Qt::ToolBarArea area, bool allow);
void place(Qt::ToolBarArea area, bool place); void place(Qt::ToolBarArea area, bool place);
QMainWindow *mainWindow;
QSpinBox *spinbox; QSpinBox *spinbox;
QAction *spinboxAction; QAction *spinboxAction;