简体   繁体   English

如何在QWidget中创建QToolBar?

[英]How to create QToolBar in QWidget?

I am trying to add a QToolBar in a QWidget . 我想在QWidget添加一个QToolBar But I want its functionality to work as if it was a QMainWindow . 但我希望它的功能像QMainWindow

Apparently I can not create QToolBar in a QWidget , and using setAllowedAreas does not work with QWidget : it only works with QMainWindow . 显然,我不能创建QToolBarQWidget ,并使用setAllowedAreas不工作QWidget :它只能与工作QMainWindow Also, my QWidget is in a QMainWindow . 此外,我的QWidget位于QMainWindow

How can I create a QToolBar for my widget? 如何为我的小部件创建QToolBar

The allowedAreas property only works when the toolbar is the child of a QMainWindow . allowedAreas属性仅在工具栏是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 : 要将它添加到继承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 , eg: 更改工具栏的方向,需要调用的额外步骤setDirectiontoolbarLayout ,如:

toolbar->setOrientation(Qt::Vertical);
toolbarLayout->setDirection(QBoxLayout::LeftToRight);
//the toolbar is now on the left side of the widget, oriented vertically

QToolBar is a widget. QToolBar是一个小部件。 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. 这就是为什么,你可以添加QToolBar通过调用任何其他部件addWidget布局或通过设置QToolBar父小部件。

As you can see in documentation of QToolBar setAllowedAreas method: 正如您在QToolBar setAllowedAreas方法的文档中看到的:

This property holds areas where the toolbar may be placed. 此属性包含可放置工具栏的区域。

The default is Qt::AllToolBarAreas. 默认值为Qt :: AllToolBarAreas。

This property only makes sense if the toolbar is in a QMainWindow. 仅当工具栏位于QMainWindow中时,此属性才有意义。

That's why it is impossible to use setAllowedAreas if toolbar is not in QMainWindow. 这就是为什么如果工具栏不在QMainWindow中则不可能使用setAllowedAreas

As far as I know, the only way to properly use the toolbar is with the QMainWindow . 据我所知,正确使用工具栏的唯一方法是使用QMainWindow

If you want to use the full functionality of the toolbar, create a mainwindow with the window flag Widget . 如果要使用工具栏的完整功能,请使用窗口标志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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM