1.导出Person类,并且一个Job类,Job类包含一个Person的指针
2.具体导出过程
1.通过属性来实现,具体的请看代码
3.具体代码
// person.h #ifndef PERSON_H #define PERSON_H #include <QObject> #include <QColor> class Person : public QObject { Q_OBJECT // 设置设置属性的名字是 name // 对应读取函数名字 getName // 对应写函数名字 setName // 属性发生改变后发送信号 sendNameChange Q_PROPERTY(QString name READ getName WRITE setName NOTIFY sendNameChange) // 设置设置属性的名字是 age // 对应读取函数名字 getAge // 对应写函数名字 setAge // 属性发生改变后发送信号 sendAgeChange Q_PROPERTY(int age READ getAge WRITE setAge NOTIFY sendAgeChange) public: explicit Person(QObject *parent = 0); QString getName(void) const; void setName(const QString& name); int getAge(void); void setAge(int age); signals: void sendNameChange(void); void sendAgeChange(void); private: QString m_Name; int m_Age; }; /* 设想一份工作给予一个人 */ class Job : public QObject { Q_OBJECT Q_PROPERTY(Person *per READ getPerson WRITE setPerson NOTIFY sendPersonChange) Q_PROPERTY(QString jn READ getJobName WRITE setJobName NOTIFY sendJobNameChange) public: explicit Job(QObject *parent = 0); ~Job(); void setPerson(Person *per); Person *getPerson(void) const; void setJobName(const QString & jobname); QString getJobName(void) const; signals: void sendPersonChange(); void sendJobNameChange(); private: Person *m_Person; QString m_JobName; }; #endif // PERSON_H |
// person.cpp #include "person.h" //--------------------------------- // Person::Person(QObject *parent) : QObject(parent), m_Name("unknow"), m_Age(0) { } //--------------------------------- // QString Person::getName(void) const { return m_Name; } //--------------------------------- // void Person::setName(const QString& name) { m_Name = name; emit sendNameChange(); } //--------------------------------- // int Person::getAge(void) { return m_Age; } //--------------------------------- // void Person::setAge(int age) { m_Age = age; emit sendAgeChange(); } //--------------------------------- // Job::Job(QObject *parent) :QObject(parent), m_Person(0), m_JobName("unknown") { } //--------------------------------- // Job::~Job() { } //--------------------------------- // void Job::setPerson(Person *per) { m_Person = per; emit sendPersonChange(); } //--------------------------------- // Person *Job::getPerson(void) const { return m_Person; } //--------------------------------- // void Job::setJobName(const QString & jobname) { m_JobName = jobname; emit sendJobNameChange(); } //--------------------------------- // QString Job::getJobName(void) const { return m_JobName; } |
// 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>(); qmlRegisterType<Job>("People",1,0,"Job"); 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>(); 导出就可以注释这条 Rectangle { width: 640 height: 480 Job { id: jobA; jn: "Learn"; per: Person { id: ps; name: "Luly"; age: 25; } } // 显示这份工作的一些信息 Rectangle{ x: 100; y: 100; width: 100; height: 100; Text { text: "Job name:" + jobA.jn; } Text { y: 20; text: "Person name:" + ps.name; } Text { y: 40; text: "Person age:" + ps.age; } } MouseArea{ anchors.fill: parent; onClicked:{ // 我要改变工作的名字 工作人的信息 jobA.jn = "Clean House"; ps.name = "Tom"; ps.age = 30; } } } |
说明:
主要是导出了两个类Person和Job, Job 包含一个Person的指针,这样后,可以看到
在QML中,我们需要给予Job对象一个Person来尽心赋值。