0
我是QML的新手,无法解决一个简单的问题。我想从C++访问QML组件,但我不能。 指针test
总是0.可能是什么原因?从C++访问现有的QML组件
的代码如下:
的main.cpp
int main(int argc, char *argv[])
{
QGuiApplication &app=reg6::Bonder::BonderGuiApplication::instance();
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject* test=engine.rootObjects().first()->findChild<QObject*> ("cameraArea");
test->setProperty("color","black");
return app.exec();
}
main.qml
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 1800
height: 900
SplitView
{
anchors.fill: parent
orientation: Qt.Vertical
SplitView {
Layout.fillHeight: true
SplitView {
orientation: Qt.Vertical
width:400
Layout.minimumWidth: 400
Layout.maximumWidth: 500
Camera {
id: cameraArea
height: 400
Layout.maximumHeight: 400
Layout.minimumHeight: 300
}
List {
id: listArea
}
}
Bonder {
id: mainArea
Layout.fillWidth: true
}
Properties {
id: propertiesArea
Layout.minimumWidth: 300
Layout.maximumWidth: 400
}
}
Error {
id: errorArea
Layout.minimumHeight: 100
height: 200
}
}
}
Camera.qml
import QtQuick 2.5
Rectangle {
color: "lightblue"
}
我想回答这样的问题可以没有来自文档的**警告**是完整的。它只是链接标题上方的段落[here](http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#accessing-members-of-a-qml-object-type-from -C)。它始于:**警告:** *虽然可以使用C++访问和操作深入对象树中的QML对象,但我们建议您不要在应用程序测试和原型设计之外采用此方法。 ] * – derM
@derM事实上,相反的方式要好得多。 –
@derM你是对的,但它不回答问题** Lecko **问,所以现在的答案是完整的。 – Redanium