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>
This commit is contained in:
Tor Arne Vestbø 2023-02-19 22:36:12 +01:00
parent c9df9f3976
commit f99df42646
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)
{ 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);
}

View File

@ -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;