How to embed QML toolbar and menubar into QMainWindow

我们两清 提交于 2019-12-22 12:26:17

问题


I am using QWT library to plot data. It seems that it is not possible to embed QWidget into QML Quick 2. So, I decided to create QMainWindow as my main window and create its toolbar and menubar using Quick Controls. How should I embed that qml toolbar and menubar into QMainWindow?


回答1:


You should create QML ApplicationWindow with QML MenuBar and ToolBar

main.qml

ApplicationWindow {
    visible: false
    menuBar: MenuBar {
        Menu {
            title: "Edit"
            MenuItem {
                text: "Cut"
            }
        }
    }
    toolBar: ToolBar {
        Row {
            anchors.fill: parent
            ToolButton {
                iconSource: "1.png"
            }
        }
    }
}

main.cpp

QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

Then get pointer to your ApplicationWindow

QWindow *qmlWindow = qobject_cast<QWindow*>(engine.rootObjects().at(0));

Create window container, by using QWidget::createWindowContainer

QWidget *container = QWidget::createWindowContainer(qmlWindow);
container->setMinimumSize(qmlWindow->size());

And place container to the top of your widget

QWidget *widget = new QWidget();
QGridLayout *grid = new QGridLayout(widget);
grid->addWidget(container,0,0);
grid->addWidget(new QPushButton(widget),1,0);
widget->show();


来源:https://stackoverflow.com/questions/27167439/how-to-embed-qml-toolbar-and-menubar-into-qmainwindow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!