QT: c++ 中通过objectName访问qml中的对象

=================================================
// c++
=================================================
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QObject>
#include <QGraphicsObject>
#include <QDeclarativeItem>


int main(int argc, char *argv[]) { QApplication app(argc, argv); QDeclarativeView view; view.setSource(QUrl::fromLocalFile("main.qml")); view.show(); QDeclarativeItem *item = view.rootObject()->findChild<QDeclarativeItem*>("myRect"); if (item) { item->setProperty("color", QColor(Qt::yellow)); } return app.exec(); }

====================================
// qml 界面显示 main.qml
===================================
import Qt 4.7 Item { width: 200; height: 200 Rectangle { anchors.fill: parent color: "red" objectName: "myRect" } }




作者: wumao2   发布时间: 2010-10-16