我尝试嵌入这个qml定义:
import QtQuick 2.0
Rectangle {
id: mqldefinition
color: "green"
width: 100
height: 100
Text {
id: text
text: "This is a text!"
font.pointSize: 14
anchors.centerIn: parent
}
}
在Qt C ++中的- 小部件:
qmlwidget.h
#include <QQuickWidget>
class QmlWidget
: public QQuickWidget
{
Q_OBJECT
public:
explicit QmlWidget(QWidget *parent = nullptr);
};
qmlwidget.h
#include "qmlwidget.h"
QmlWidget::QmlWidget(QWidget *parent)
: QQuickWidget(parent)
{
resize(100,100);
setSource(QUrl("qrc:/definition.qml"));
}
此小部件应显示在MainWindow中,部分显示在此处:
mainwindow.cpp
#include "mainwindow.h"
#include "qmlwidget.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
auto qmlWidget = new QmlWidget(this);
qmlWidget->move(0,0);
}
但未显示:(
我在SO中找到了这个:
QQuickWidget *view = new QQuickWidget;
view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));
view->show();
但我想将其作为派生类实现:)