qml中去使用c++类

C++导出到QML的过程。

 

1.导出一个简单的类Person

2.具体导出过程

假设我们要导出一个Person类,

A那么就要考虑如何的一个类他才可以导出呢?

他需要符合一定的条件

1.继承自QObject

2.有默认构造函数

B如何导出呢?

通过一个函数

int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)

int qmlRegisterType()

3.具体的例子

 

//person.h
#ifndefPERSON_H
#definePERSON_H
#include<QObject>
classPerson:publicQObject
{
Q_OBJECT
public:
explicitPerson(QObject*parent=0);
};
#endif//PERSON_H

 

//person.cpp
#include"person.h"
Person::Person(QObject*parent):
QObject(parent)
{
}

 

//main.cpp
#include<QtGui/QApplication>
#include<QtDeclarative/QDeclarativeView>
#include<QtDeclarative/QDeclarativeEngine>
#include<QtDeclarative/QDeclarativeComponent>
#include"person.h"
intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
qmlRegisterType<Person>("People",1,0,"Person");
//qmlRegisterType<Person>();
QDeclarativeViewqmlView;
qmlView.setSource(QUrl::fromLocalFile("../UICtest/UICtest.qml"));
qmlView.show();
returna.exec();
}

 

//UICtest.qml
importQt4.7
importPeople1.0//如果是qmlRegisterType<Person>();导出就可以注释这条
Rectangle{
width:640
height:480
Person{}
}

说明:我们通过qmlRegisterType<Person>("People",1,0,"Person");

QML中导出Person,这个类在People包中,在QML中需要使用Person类的

话就必须包含People,通过importPeople1.0来包含,之后就可以使用Person

创建对象使用来

posted on 2013-02-22 19:06  小风儿_xf  阅读(1682)  评论(0编辑  收藏  举报

导航