The allowedAreas
property only works when the toolbar is the child of a QMainWindow
. You can add the toolbar to a layout, but it won't be movable by the user. You can still relocate it programmatically, however.
To add it to a layout for a fictional class inheriting QWidget
:
void SomeWidget::setupWidgetUi()
{
toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
//set margins to zero so the toolbar touches the widget's edges
toolLayout->setContentsMargins(0, 0, 0, 0);
toolbar = new QToolBar;
toolLayout->addWidget(toolbar);
//use a different layout for the contents so it has normal margins
contentsLayout = new ...
toolLayout->addLayout(contentsLayout);
//more initialization here
}
Changing the toolbar's orientation requires the additional step of calling setDirection
on the toolbarLayout
, e.g.:
toolbar->setOrientation(Qt::Vertical);
toolbarLayout->setDirection(QBoxLayout::LeftToRight);
//the toolbar is now on the left side of the widget, oriented vertically
QToolBar
child widget to a parentQWidget
container. You cannot, however, do so graphically from within either Qt Creator or Designer. For unknown reasons (presumably relating to scarce developer resources), both permitQToolBar
widgets to be created only by right clicking on aQMainWindow
instance and selecting Add Tool Bar. You can probably circumvent this arbitrary constraint by manually editing your project's XML-formatted.ui
file and adding in an appropriate<widget class="QToolBar"...>
tag – but do so with care! – Langtry