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?

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
njporwal
  • 111
  • 2
  • 4
  • 9
  • 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? – peppe Jul 15 '16 at 09:45
  • @peppe I was going to give this 'hint' as an answer. Do you care doing this? – IAmInPLS Jul 15 '16 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 `` tag – but do so with care! – Cecil Curry Feb 28 '18 at 04:46

3 Answers3

11

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
jonspaceharper
  • 4,207
  • 2
  • 22
  • 42
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.

Kirill Chernikov
  • 1,387
  • 8
  • 21
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);
}
Felix
  • 6,885
  • 1
  • 29
  • 54
  • 2
    **No.** As [Kirill Chernikov](https://stackoverflow.com/users/6367468/kirill-chernikov)'s [answer](https://stackoverflow.com/a/38392748/2809027) 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." – Cecil Curry Feb 28 '18 at 04:38
  • using the `Widget` flag causes the `QMainWindow` to stop showing `QToolBar`s. – Mohammad Rahimi Jun 07 '21 at 07:51