6

I am trying to add a QToolBar in a QWidget. But I want its functionality to work as if it was a QMainWindow.

Apparently I can not create QToolBar in a QWidget, and using setAllowedAreas does not work with QWidget : it only works with QMainWindow. Also, my QWidget is in a QMainWindow.

How can I create a QToolBar for my widget?

3
  • 4
    So you have a widget in a QMainWindow but want to add toolbars to that widget, not to the QMainWindow? How about making you widget a QMainWindow subclass? <inception horn>
    – peppe
    Jul 15, 2016 at 9:45
  • @peppe I was going to give this 'hint' as an answer. Do you care doing this?
    – IAmInPLS
    Jul 15, 2016 at 10:14
  • You can programmatically add a QToolBar child widget to a parent QWidget container. You cannot, however, do so graphically from within either Qt Creator or Designer. For unknown reasons (presumably relating to scarce developer resources), both permit QToolBar widgets to be created only by right clicking on a QMainWindow 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! Feb 28, 2018 at 4:46

3 Answers 3

12

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
2

QToolBar is a widget. That's why, you can add a QToolBar to any other widget by calling addWidget for layout or by setting the QToolBar parent to your widget.

As you can see in documentation of QToolBar setAllowedAreas method:

This property holds areas where the toolbar may be placed.

The default is Qt::AllToolBarAreas.

This property only makes sense if the toolbar is in a QMainWindow.

That's why it is impossible to use setAllowedAreas if toolbar is not in QMainWindow.

0

As far as I know, the only way to properly use the toolbar is with the QMainWindow.

If you want to use the full functionality of the toolbar, create a mainwindow with the window flag Widget. This way you can add it inside some other widget without having it displayed as a new window:

class MyWidget : QMainWindow
{
public:
    MyWidget(QWidget *parent);
    //...

    void addToolbar(QToolBar *toolbar);

private:
    QMainWindow *subMW;
}

MyWidget::MyWidget(QWidget *parent)
    QMainWindow(parent)
{
    subMW = new QMainWindow(this, Qt::Widget);//this is the important part. You will have a mainwindow inside your mainwindow
    setCentralWidget(QWidget *parent);
}

void MyWidget::addToolbar(QToolBar *toolbar)
{
    subMW->addToolBar(toolbar);
}
2
  • 2
    No. As Kirill Chernikov's answer suggests, QToolBar subclasses QWidget and hence is generically reusable as a standard widget. Note, however, the following caveat from the official documentation: "When a QToolBar is not a child of a QMainWindow, it loses the ability to populate the extension pop up with widgets added to the toolbar using addWidget(). Please use widget actions created by inheriting QWidgetAction and implementing QWidgetAction::createWidget() instead." Feb 28, 2018 at 4:38
  • using the Widget flag causes the QMainWindow to stop showing QToolBars. Jun 7, 2021 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.