且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C ++代码中将qml类型用作QWindow

更新时间:2023-02-19 14:30:41

您可以使用QQuickWidget,但请记住,QML的根必须是Item或从Item继承的类,不能是Window或ApplicationWindow.

You can use QQuickWidget but remember that the root of the QML must be an Item or a class that inherits from the Item, it can not be Window or ApplicationWindow.

#include <QApplication>
#include <QMainWindow>
#include <QMdiArea>
#include <QQuickWidget>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QApplication app(argc, argv);
    QMainWindow w;
    QMdiArea *mdiarea = new QMdiArea;
    w.setCentralWidget(mdiarea);
    QQuickWidget *toolbar = new QQuickWidget(QUrl("qrc:/main.qml"));
    toolbar->setResizeMode(QQuickWidget::SizeRootObjectToView);
    mdiarea->addSubWindow(toolbar);
    w.show();
    return app.exec();
}

main.qml

import QtQuick 2.9
import QtQuick.Controls 2.4

Rectangle {
    visible: true
    width: 640
    height: 480
    color: "red"
    Button{
        text: "Stack Overflow"
        anchors.centerIn: parent
    }
}