Search code examples
qtqt5qmainwindowqdockwidgetqtoolbar

Can a QToolbar be added to a QDockWidget?


I have setup my app to have various dock windows within the main window. I am also able to add a toolbar to the main window. However, I would ideally like to add the QToolBar inside one of the QDockWindow instances (or the QWidget that it houses) as the toolbar will be specific to that window.

Is this possible? I'm using a recent version of Qt, 5.10.


Solution

  • I think it is possible.

    1.QDockWidget can set a QMainWindow by setWidget() method. QMainWindow is made for just a mainwindow but it is not prevented from being used as a subwidget.

    2.QToolBar can be attached to the main-subwindow by addToolBar() method.

    3.The subwidget-mainwindow can naturally have its own QToolbar.

    If you don't want to use QMainWindow as the widget of its QDockWidget,you can attach the QToolBar as a child widget of QDockWidget. But The toolbar is not movable as QMainWindow's.

    I think you want to add QToolBar and use it as QMainWindow. So I recommend that you set a QMainWindow as the widget of QDockWidget.And you attach any widget you like to the mainwindow after that.