多肉生物 |
2017-02-12 13:02 |
继续回答。 //main.cpp #include <QGuiApplication>
#include <QtQml>//想读、写qml对象的属性你就得包含它。
int main(int argc, char *argv[]) { //QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv);
qmlRegisterType<Myclass>("myclass.namespace", 1, 0, "Myclass");//用以在QML中实用C++类的实例。当然这不是唯一方法,推荐使用这种方法,因为它会让你的C++实例看起来像QML原生的。 QQmlEngine engine; QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:///main.qml"))); QObject *object = component.create(); QObject *rect = object->findChild<QObject*>("your_object_name"); if(rect) { qDebug()<<"find the object"; rect->setProperty("text", "yourid"); } return app.exec(); } //main.qml import QtQuick 2.7 import QtQuick.Controls 2.0 //2.0 没有tableview import QtQuick.Layouts 1.0 import QtQuick.Window 2.1 ApplicationWindow { visible: true width: 640 height: 480 Myclass { id:myclass onBegin : doSomething() } Rectangle { //anchors.fill: parent
width: 640 height: 480 id: display Label{objectName: "your_object_name"}
} |
|