How to create QToolBar in QWidget?
Asked Answered
H

4

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?

Heeltap answered 15/7, 2016 at 9:16 Comment(3)
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>Insulation
@Insulation I was going to give this 'hint' as an answer. Do you care doing this?Sturgeon
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!Langtry
S
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
Serrato answered 15/7, 2016 at 9:41 Comment(0)
B
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.

Barrator answered 15/7, 2016 at 9:36 Comment(0)
G
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);
}
Gillman answered 15/7, 2016 at 14:19 Comment(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."Langtry
using the Widget flag causes the QMainWindow to stop showing QToolBars.Antagonist
D
0

It's actually easy to do in designer by fooling it temporarily into thinking your widget is a QMainWindow.

  1. Close the widget in designer

  2. Using a text editor, open mywidget.ui and change the line near the top which reads:

<widget class="QWidget" name="MyWidget">

to read

<widget class="QMainWindow" name="MyWidget">

  1. Save the file and re-open in Qt Designer. You will now have the option to add a toolbar.

  2. After you have added the toolbar, reverse the process changing the widget in the mywidget.ui back to QWidget.

All done, and you now have a nice, visually editable toolbar in your widget.

Dortch answered 23/5 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.