If you don't need the built-in drag and drop feature of QMainWindow's toolbars, you can simply add a QToolBar to any layout, including QDialog's layout(). See the DigviJay Patil's answer below for details, which is definitely cleaner conceptually.
Otherwise, please read on.
It is not directly possible to add a
QToolBar
to aQDialog
in the QMainWindow::addToolBar() sense, becauseQDialog
inherits onlyQWidget
and notQMainWindow
, as you noted (hence do not have the methodaddToolBar()
)You can't make a
QMainWindow
modal, but you can insert aQMainWindow
in aQDialog
this way:
Code:
MyDialog::MyDialog() :
QDialog()
{
QMainWindow * mainWindow = new QMainWindow(); // or your own class
// inheriting QMainWindow
QToolBar * myToolBar = new QToolBar();
mainWindow->addToolBar(myToolBar);
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(mainWindow);
setLayout(layout);
}
Indeed, a QMainWindow
doesn't necessarily have to be a top-level widget, and you can even insert several QMainWindow
s as children of a single widget (may not be the wisest choice though, as the user would probably be confused with the separate sets of menu bars, toolbars, dock widgets, etc.).