首先说明什么事QML?百度百科是这样描述:
QML是Qt推出的Qt Quick技术的一部分,是一种新增的简便易学的语言。QML是一种陈述性语言,用来描述一个程序的用户界面:无论是什么样子,以及它如何表现。在QML,一个用户界面被指定为具有属性的对象树。 这使得Qt更加便于很少或没有编程经验的人使用。
QML实际上是Qt Quick (Qt4.7.0中的新特性)核心组件之一:Qt Quick是一组旨在帮助开发者创建在移动电话,媒体播放器,机顶盒和其他便携设备上使用越来越多的直观、现代、流畅UI的工具集合。
我们可以将C++定义的类型的类型注册到QML,在QML中使用这个类型。首先看看几个关键的东东
int qmlRegisterType ( const char * uri, int versionMajor, int versionMinor, const char * qmlName )
上面这个模板函数就是用来向QML注册C++类型。
下面一个例子就是向QML注册Person类型。
-
person.h
-
--------------------------------------
-
#ifndef PERSON_H
-
#define PERSON_H
-
-
#include
-
-
class Person : public QObject
-
{
-
Q_OBJECT
-
Q_PROPERTY(QString name READ name WRITE setName)
-
Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
-
public:
-
Person(QObject *parent = 0);
-
QString name() const;
-
void setName(const QString &);
-
int shoeSize() const;
-
void setShoeSize(int);
-
-
private:
-
QString m_name;
-
int m_shoeSize;
-
};
-
-
#endif // PERSON_H
-
-
-
person.cpp
-
---------------------------------------------------
-
#include "person.h"
-
-
Person::Person(QObject *parent) : QObject(parent), m_shoeSize(0)
-
{
-
-
}
-
-
QString Person::name() const
-
{
-
return m_name;
-
}
-
-
void Person::setName(const QString &n)
-
{
-
m_name = n;
-
}
-
-
int Person::shoeSize() const
-
{
-
return m_shoeSize;
-
}
-
-
void Person::setShoeSize(int s) {
-
m_shoeSize = s;
-
}
-
-
-
-
main.cpp
-
----------------------------------------------------
-
#include
-
#include
-
#include
-
#include
-
#include "person.h"
-
-
int main(int argc, char *argv[])
-
{
-
QApplication a(argc, argv);
-
qmlRegisterType("People", 1, 0, "Person");
-
QDeclarativeEngine engine;
-
QDeclarativeComponent component(&engine, QUrl("qrc:/example.qml"));
-
Person *person = qobject_cast(component.create());
-
if(person) {
-
qWarning() << "The person's name is" << person->name();
-
qWarning() << "They wear a" << person->shoeSize() << "sized shoe";
-
} else {
-
qWarning() << component.errors();
-
}
-
-
return a.exec();
-
}
-
-
-
example.qml
-
---------------------------------------------------
-
import People 1.0
-
-
Person {
-
name: "Lavy Liu"
-
shoeSize: 12
-
}
-
-
-
-
输出:
-
The person's name is "Lavy Liu"
-
They wear a 12 sized shoe
下面的例子演示如何向利用C++自定义QML类型及自定义类型List
在Person类中用到name 和shoeSize属性,故在person.h中使用:
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
在BirthdayParty中类中用到Person类对象和一个以Person为元素的List对象 guests 作为属性且guests只读,故使用:
Q_PROPERTY(Person *host READ host WRITE setHost)
Q_PROPERTY(QDeclarativeListProperty<Person> guests READ guests)
-
person.h
-
----------------------------------------
-
#ifndef PERSON_H
-
#define PERSON_H
-
-
#include
-
-
class Person : public QObject
-
{
-
Q_OBJECT
-
Q_PROPERTY(QString name READ name WRITE setName)
-
Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
-
-
public:
-
Person(QObject *parent = 0);
-
QString name() const;
-
void setName(const QString &);
-
int shoeSize() const;
-
void setShoeSize(int);
-
private:
-
QString m_name;
-
int m_shoeSize;
-
};
-
-
#endif // PERSON_H
-
person.cpp
-
--------------------------------------------
-
#include "person.h"
-
-
Person::Person(QObject * parent) : QObject(parent), m_shoeSize(0)
-
{
-
}
-
-
QString Person::name() const
-
{
-
return m_name;
-
}
-
-
void Person::setName(const QString &n) {
-
m_name = n;
-
}
-
-
int Person::shoeSize() const{
-
return m_shoeSize;
-
}
-
-
void Person::setShoeSize(int s) {
-
m_shoeSize = s;
-
}
-
birthday.h
-
-----------------------------------------------
-
#ifndef BIRTHDAYPARTY_H
-
#define BIRTHDAYPARTY_H
-
-
#include
-
#include
-
#include "person.h"
-
-
class BirthdayParty : public QObject
-
{
-
Q_OBJECT
-
Q_PROPERTY(Person *host READ host WRITE setHost)
-
Q_PROPERTY(QDeclarativeListProperty guests READ guests)
-
-
public:
-
BirthdayParty(QObject *parent = 0);
-
Person *host() const;
-
void setHost(Person *);
-
QDeclarativeListProperty guests();
-
int guestCount() const;
-
Person *guest(int) const;
-
private:
-
Person *m_host;
-
QList m_guests;
-
};
-
-
#endif // BIRTHDAYPARTY_H
-
birthday.cpp
-
------------------------------------------
-
#include "birthdayparty.h"
-
-
BirthdayParty::BirthdayParty(QObject *parent) : QObject(parent), m_host(0)
-
{
-
}
-
-
Person *BirthdayParty::host() const {
-
return m_host;
-
}
-
-
void BirthdayParty::setHost(Person * c) {
-
m_host = c;
-
}
-
-
QDeclarativeListProperty BirthdayParty::guests()
-
{
-
return QDeclarativeListProperty(this, m_guests);
-
}
-
-
int BirthdayParty::guestCount() const
-
{
-
return m_guests.count();
-
}
-
-
Person *BirthdayParty::guest(int index) const
-
{
-
return m_guests.at(index);
-
}
-
main.cpp
-
------------------------------------------
-
#include
-
#include
-
#include
-
#include
-
#include "birthdayparty.h"
-
#include "person.h"
-
-
int main(int argc, char ** argv)
-
{
-
QApplication app(argc, argv);
-
-
qmlRegisterType("People", 1,0, "BirthdayParty");
-
qmlRegisterType("People", 1,0, "Person");
-
-
QDeclarativeEngine engine;
-
QDeclarativeComponent component(&engine, QUrl("qrc:/example.qml"));
-
BirthdayParty *party = qobject_cast(component.create());
-
-
if (party && party->host()) {
-
qWarning() << party->host()->name() << "is having a birthday!";
-
qWarning() << "They are inviting:";
-
for (int i = 0; i < party->guestCount(); ++i)
-
qWarning() << " " << party->guest(i)->name() << party->guest(i)->shoeSize();
-
} else {
-
qWarning() << component.errors();
-
}
-
-
return app.exec();
-
}
-
example.qml
-
-----------------------------------------------
-
import People 1.0
-
-
BirthdayParty {
-
host : Person {
-
name: "Lavy Liu"
-
shoeSize: 12
-
}
-
guests: [
-
Person { name: "Sglaze"; shoeSize: 18},
-
Person { name: "liuhongwei"; shoeSize: 23},
-
Person { name: "liuhw"}
-
]
-
}
输出:
"Lavy Liu" is having a birthday!
They are inviting:
"Sglaze" 18
"liuhongwei" 23
"liuhw" 0
接下来的代码是增加两个Person的子类,Boy和Girl,并分别注册到QML.
person.h
-
#ifndef PERSON_H
-
#define PERSON_H
-
-
#include
-
-
class Person : public QObject
-
{
-
Q_OBJECT
-
-
Q_PROPERTY(QString name READ name WRITE setName)
-
Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
-
-
public:
-
Person(QObject *parent = 0);
-
QString name() const;
-
void setName(const QString &);
-
int shoeSize() const;
-
void setShoeSize(int);
-
-
private:
-
QString m_name;
-
int m_shoeSize;
-
};
-
-
class Boy : public Person
-
{
-
Q_OBJECT
-
public:
-
Boy(QObject * parent = 0);
-
};
-
-
class Girl : public Person
-
{
-
Q_OBJECT
-
public:
-
Girl(QObject * parent = 0);
-
};
-
-
#endif // PERSON_H
person.cpp
-
#include "person.h"
-
-
Person::Person(QObject *parent) : QObject(parent), m_shoeSize(0)
-
{
-
}
-
-
void Person::setName(const QString & name)
-
{
-
m_name = name;
-
}
-
-
QString Person::name() const
-
{
-
return m_name;
-
}
-
-
void Person::setShoeSize(int size)
-
{
-
m_shoeSize = size;
-
}
-
-
int Person::shoeSize() const {
-
return m_shoeSize;
-
}
-
-
Boy::Boy(QObject *parent) : Person(parent)
-
{
-
-
}
-
-
Girl::Girl(QObject *parent) : Person(parent)
-
{
-
-
}
birthdayparty.h
-
#ifndef BIRTHDAYPARTY_H
-
#define BIRTHDAYPARTY_H
-
#include
-
#include
-
#include "person.h"
-
-
class BirthdayParty : public QObject
-
{
-
Q_OBJECT
-
-
Q_PROPERTY(Person *host READ host WRITE setHost)
-
Q_PROPERTY(QDeclarativeListProperty guests READ guests)
-
-
public:
-
BirthdayParty(QObject *parent = 0);
-
Person *host() const;
-
void setHost(Person *);
-
-
QDeclarativeListProperty guests();
-
int guestCount() const;
-
Person *guest(int) const;
-
private:
-
Person * m_host;
-
QList m_guests;
-
};
-
-
#endif // BIRTHDAYPARTY_H
birthdayparty.cpp
-
#include "birthdayparty.h"
-
-
BirthdayParty::BirthdayParty(QObject *parent) : QObject(parent),m_host(0)
-
{
-
}
-
-
Person *BirthdayParty::host() const
-
{
-
return m_host;
-
}
-
-
void BirthdayParty::setHost(Person *c) {
-
m_host = c;
-
}
-
-
QDeclarativeListProperty BirthdayParty::guests()
-
{
-
return QDeclarativeListProperty(this, m_guests);
-
}
-
-
int BirthdayParty::guestCount() const
-
{
-
return m_guests.count();
-
}
-
-
Person *BirthdayParty::guest(int index) const
-
{
-
return m_guests.at(index);
-
}
main.cpp
-
#include
-
#include
-
#include
-
#include
-
#include "birthdayparty.h"
-
#include "person.h"
-
-
int main(int argc, char *argv[])
-
{
-
QApplication a(argc, argv);
-
-
-
qmlRegisterType("People", 1, 0, "BirthdayParty");
-
qmlRegisterType();
-
qmlRegisterType("People", 1, 0, "Boy");
-
qmlRegisterType("People", 1, 0, "Girl");
-
-
QDeclarativeEngine engine;
-
QDeclarativeComponent component(&engine, QUrl("qrc:/example.qml"));
-
-
BirthdayParty *party = qobject_cast(component.create());
-
if(party && party->host())
-
{
-
qWarning() << party->host()->name() <<"is having a birthday!";
-
if(qobject_cast(party->host()))
-
qWarning() << "He is inviting:";
-
else
-
qWarning() << "She is inviting:";
-
for(int i = 0; i < party->guestCount(); ++i)
-
{
-
qWarning() << " " << party->guest(i)->name();
-
}
-
} else
-
{
-
qWarning() << component.errors();
-
}
-
return a.exec();
-
}
example.qml
-
import People 1.0
-
-
BirthdayParty {
-
host : Boy {
-
name: "Lavy Liu"
-
shoeSize: 12
-
}
-
guests: [
-
Boy { name: "Sglaze"; shoeSize: 18},
-
Girl { name: "Lucy"; shoeSize: 20},
-
Girl { name: "Lily"}
-
]
-
}
输出:
"Lavy Liu" is having a birthday!
He is inviting:
"Sglaze"
"Lucy"
"Lily"
阅读(2968) | 评论(0) | 转发(0) |