Using a qml type as a QWindow in C++ code

谁说我不能喝 提交于 2020-01-30 08:51:07

问题


I've created a MainWindow : public QMainWindow and a qtquick ui file (for a toolbox) in qtcreator. I want the toolbox to appear as a floating subwindow in mainwindow. I'm trying to use QMdiArea for that. A tutorial I've seen says that I need to add a window to the QMdiArea like this:

mdi->addSubWindow(win);

where win is a QWidget. How do I use the toolbox created with qml in my C++ code?


回答1:


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
    }
}



来源:https://stackoverflow.com/questions/53611468/using-a-qml-type-as-a-qwindow-in-c-code

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