關(guān)于導(dǎo)出C++的學(xué)習(xí)
說(shuō)明,主要是對(duì)QT的文檔內(nèi)例子進(jìn)行的一些分別解說(shuō),希望更容易的理解
C++導(dǎo)出到QML的過(guò)程。
1.導(dǎo)出一個(gè)簡(jiǎn)單的類Person
2.具體導(dǎo)出過(guò)程
假設(shè)我們要導(dǎo)出一個(gè)Person類,
A 那么就要考慮如何的一個(gè)類他才可以導(dǎo)出呢?
他需要符合一定的條件
1.繼承自QObject
2.有默認(rèn)構(gòu)造函數(shù)
B 如何導(dǎo)出呢?
通過(guò)一個(gè)函數(shù)
int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
int qmlRegisterType()
3.具體的例子
// person.h
#ifndef PERSON_H
#define PERSON_H
#include <QObject>
class Person : public QObject
{
Q_OBJECT
public:
explicit Person(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"
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();
} |
// UICtest.qml
import Qt 4.7
import People 1.0 //如果是qmlRegisterType<Person>(); 導(dǎo)出就可以注釋這條
Rectangle {
width: 640
height: 480
Person{}
} |
說(shuō)明:我們通過(guò)qmlRegisterType<Person>("People",1,0,"Person");
向QML中導(dǎo)出Person類,這個(gè)類在People包中,在QML中需要使用Person類的
話就必須包含People包,通過(guò)import People 1.0來(lái)包含,之后就可以使用Person
創(chuàng)建對(duì)象使用來(lái)。