这个是转载的,【lvliang2008】,
http://blog.csdn.net/lvliang2008/article/details/6307362
写的非常的详细了。
在注册到qml中的时候还是有一些坑的。
#include <QtGui/QApplication>
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeComponent>
#include "person.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qmlRegisterType<Person>("People",1,0,"Person");
//qmlRegisterType<Person>();
QDeclarativeView qmlView;
qmlView.setSource(QUrl::fromLocalFile("../UICtest/UICtest.qml"));
qmlView.show();
return a.exec();
}
QML的显示方式大致有两种,一种是QDeclarativeView qmlView; 和QQuickView的方式,这种使用 qmlRegisterType(“People”,1,0,”Person”); 注册然后在qml中import后,就可以使用了。
第二种是 QQmlApplicationEngine engine;使用qmlRegisterType能注册,但是就是用不了,不知道为什么。然后使用
engine.rootContext()->setContextProperty(“imageprocess”,new imageProcess);
这个方式,在qml就是像对象一样的使用了。