auto box = new QVBoxLayout();
qml = new QQuickWidget();
setLayout(box);
qml->setResizeMode(QQuickWidget::SizeRootObjectToView);
qml->setClearColor(Qt::gray);
QMLObjectInterface* face = new QMLObjectInterface(this);
qml->engine()->rootContext()->setContextProperty("cplusplusObject",face);
qml->setSource(QUrl("QML.qml"));
foreach(auto error, qml->errors())
qDebug() << "error:" << error;
auto object = qml->rootObject();
object->setProperty("width",480);
auto items = object->findChildren<QQuickItem*>("button");
if (!items.isEmpty())
items[0]->setProperty("text",QStringLiteral("按键"));
box->addWidget(qml);
QML
import QtQuick 2.11
import QtQuick.Controls 2.1
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.3
import QtQuick.Window 2.11
import "qmls" as QMLS
import cplusplus 1.0
Rectangle
{
id: base
width:640
height:480
ColumnLayout
{
anchors.fill:parent
anchors.margins: 6
spacing: 4
Rectangle
{
id:image
Layout.fillWidth: true
Layout.fillHeight: true
color:"#cdcdc0"
TextEdit
{
id:text
anchors.centerIn: parent
width:parent.width
text:"请点击下方按键"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.WordWrap
font.pointSize: 12
}
}
RowLayout
{
Rectangle
{
Layout.fillWidth: true
}
Button
{
id:button
property int index: 0
property string buttonText:"Click"
text:buttonText
objectName: "button"
onClicked:
{
text.text = cplusplusObject.getString()
}
}
}
}
}