I've been trying to learn QtQuick for GUI creation, but I've been having a hard time understanding how to interact with QML objects from the C++ part of my test program.
Here's my simple QML file:
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
id: mainWindow
visible: true
width: 800
height: 800
color: "#FFFF0000"
MouseArea {
anchors.fill: parent
onClicked: Qt.quit()
}
Rectangle {
id: testRect
width: 100
height: 100
anchors.centerIn: parent
color: "#FF0000FF"
}
}
Here's the basic C++ file that came with it (auto-generated by QtCreator):
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
My issue is that I have no idea how to gain access to my 'Window' QML object, and as a result, I'm unable to alter any of its properties or the properties of its children! This part of the QtQuick documentation shows two methods of accessing QML objects from within C++ code, but neither of them seem to apply to this 'QQmlApplicationEngine' loading scheme.. I've also seen people use things like 'QApplicationViewer' and 'QDeclaritiveView', but I can't seem to find those in the official documentation at all..
I'm getting really frustrated with QtQuick; the 'simplicity' of QML seems to be lost in a sea of conflicting documentation and convoluted interface between C++ and QML. Is there anyway for me to access my QML objects while using the QQmlApplicationEngine method? I've tried using 'QuickView', but it doesn't seem to work well with Window QML objects..? Is QQmlApplicationEngine only useful for QML-only applications in a single file? So far, every piece of documentation and tutorial I've read has shown something different..
Any help or clarification would be appreciated. Ideally I'd like to know how to access and modify QML objects (like 'mainWindow', 'testRect', and other objects in other files) via my C++ code.
question from:
https://stackoverflow.com/questions/23177839/how-can-i-access-my-window-object-properties-from-c-while-using-qqmlapplicatio 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…