步骤一
新建一个C++带着UI的工程
第二步
在C++工程的pro中添加
QT +=quickwidgets
在头文件中添加
#include <QUrl>
#include <QtQuickWidgets/QQuickWidget>
第三步
我这里是先新建一个qml文件再添加到c++工程的qrc资源文件中的,这里应该还有其他办法这个我现在先这样实验简单
其中代码如下,先定义一个矩形框,然后装一个Text文件
import QtQuick 2.0
Rectangle{
id:page
width: 500; height: 200
color: "paleturquoise"
Text {
id: helloText
text: qsTr("helloword")
y: 30
anchors.horizontalCenter: page.horizontalCenter
font.pointSize: 24; font.bold: true
}
}
第四步
在C++的mainwindow.cpp中添加
QQuickWidget *qw = new QQuickWidget(this);
qw->move(50,50);
// qw->resize(100,100);
qw->setSource(QUrl("qrc:/qml/hello.qml"));
之后的运行效果图如下所示