Chinaunix首页 | 论坛 | 博客
  • 博客访问: 708137
  • 博文数量: 67
  • 博客积分: 994
  • 博客等级: 准尉
  • 技术积分: 1749
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-03 14:10
文章分类
文章存档

2014年(11)

2013年(14)

2012年(14)

2011年(28)

分类: C/C++

2014-07-21 21:28:28

首先说明什么事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类型。

  

[c-sharp] view plaincopy
  1. person.h  
  2. --------------------------------------  
  3. #ifndef PERSON_H  
  4. #define PERSON_H  
  5.  
  6. #include   
  7.   
  8. class Person : public QObject  
  9. {  
  10.     Q_OBJECT  
  11.     Q_PROPERTY(QString name READ name WRITE setName)  
  12.     Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)  
  13. public:  
  14.     Person(QObject *parent = 0);  
  15.     QString name() const;  
  16.     void setName(const QString &);  
  17.     int shoeSize() const;  
  18.     void setShoeSize(int);  
  19.   
  20. private:  
  21.     QString m_name;  
  22.     int m_shoeSize;  
  23. };  
  24.  
  25. #endif // PERSON_H  
  26.   
  27.   
  28. person.cpp  
  29. ---------------------------------------------------  
  30. #include "person.h"  
  31.   
  32. Person::Person(QObject *parent) : QObject(parent), m_shoeSize(0)  
  33. {  
  34.   
  35. }  
  36.   
  37. QString Person::name() const  
  38. {  
  39.     return m_name;  
  40. }  
  41.   
  42. void Person::setName(const QString &n)  
  43. {  
  44.     m_name = n;  
  45. }  
  46.   
  47. int Person::shoeSize() const  
  48. {  
  49.     return m_shoeSize;  
  50. }  
  51.   
  52. void Person::setShoeSize(int s) {  
  53.     m_shoeSize = s;  
  54. }  
  55.   
  56.   
  57.   
  58. main.cpp  
  59. ----------------------------------------------------  
  60. #include   
  61. #include   
  62. #include   
  63. #include   
  64. #include "person.h"  
  65.   
  66. int main(int argc, char *argv[])  
  67. {  
  68.     QApplication a(argc, argv);  
  69.     qmlRegisterType("People", 1, 0, "Person");  
  70.     QDeclarativeEngine engine;  
  71.     QDeclarativeComponent component(&engine, QUrl("qrc:/example.qml"));  
  72.     Person *person = qobject_cast(component.create());  
  73.     if(person) {  
  74.         qWarning() << "The person's name is" << person->name();  
  75.         qWarning() << "They wear a" << person->shoeSize() << "sized shoe";  
  76.     } else {  
  77.         qWarning() << component.errors();  
  78.     }  
  79.   
  80.     return a.exec();  
  81. }  
  82.   
  83.   
  84. example.qml  
  85. ---------------------------------------------------  
  86. import People 1.0  
  87.   
  88. Person {  
  89.     name: "Lavy Liu"  
  90.     shoeSize: 12  
  91. }  
  92.   
  93.   
  94.   
  95. 输出:  
  96. The person's name is "Lavy Liu"   
  97. 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) 

    

  1. person.h  
  2. ----------------------------------------  
  3. #ifndef PERSON_H  
  4. #define PERSON_H  
  5.   
  6. #include   
  7.   
  8. class Person : public QObject  
  9. {  
  10.     Q_OBJECT  
  11.     Q_PROPERTY(QString name READ name WRITE setName)  
  12.     Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)  
  13.   
  14. public:  
  15.     Person(QObject *parent = 0);  
  16.     QString name() const;  
  17.     void setName(const QString &);  
  18.     int shoeSize() const;  
  19.     void setShoeSize(int);  
  20. private:  
  21.     QString m_name;  
  22.     int m_shoeSize;  
  23. };  
  24.   
  25. #endif // PERSON_H  

 

 

    

  1. person.cpp  
  2. --------------------------------------------  
  3. #include "person.h"  
  4.   
  5. Person::Person(QObject * parent) : QObject(parent), m_shoeSize(0)  
  6. {  
  7. }  
  8.   
  9. QString Person::name() const  
  10. {  
  11.     return m_name;  
  12. }  
  13.   
  14. void Person::setName(const QString &n) {  
  15.     m_name = n;  
  16. }  
  17.   
  18. int Person::shoeSize() const{  
  19.     return m_shoeSize;  
  20. }  
  21.   
  22. void Person::setShoeSize(int s) {  
  23.     m_shoeSize = s;  
  24. }  

 

 

    

  1. birthday.h  
  2. -----------------------------------------------  
  3. #ifndef BIRTHDAYPARTY_H  
  4. #define BIRTHDAYPARTY_H  
  5.   
  6. #include   
  7. #include   
  8. #include "person.h"  
  9.   
  10. class BirthdayParty : public QObject  
  11. {  
  12.     Q_OBJECT  
  13.     Q_PROPERTY(Person *host READ host WRITE setHost)  
  14.     Q_PROPERTY(QDeclarativeListProperty guests READ guests)  
  15.   
  16. public:  
  17.     BirthdayParty(QObject *parent = 0);  
  18.     Person *host() const;  
  19.     void setHost(Person *);  
  20.     QDeclarativeListProperty guests();  
  21.     int guestCount() const;  
  22.     Person *guest(intconst;  
  23. private:  
  24.     Person *m_host;  
  25.     QList m_guests;  
  26. };  
  27.   
  28. #endif // BIRTHDAYPARTY_H  

 

 

    

  1. birthday.cpp  
  2. ------------------------------------------  
  3. #include "birthdayparty.h"  
  4.   
  5. BirthdayParty::BirthdayParty(QObject *parent) : QObject(parent), m_host(0)  
  6. {  
  7. }  
  8.   
  9. Person *BirthdayParty::host() const {  
  10.     return m_host;  
  11. }  
  12.   
  13. void BirthdayParty::setHost(Person * c) {  
  14.     m_host = c;  
  15. }  
  16.   
  17. QDeclarativeListProperty BirthdayParty::guests()  
  18.  {  
  19.      return QDeclarativeListProperty(this, m_guests);  
  20.  }  
  21.   
  22.  int BirthdayParty::guestCount() const  
  23.  {  
  24.      return m_guests.count();  
  25.  }  
  26.   
  27.  Person *BirthdayParty::guest(int index) const  
  28.  {  
  29.      return m_guests.at(index);  
  30.  }  

 

 

    

  1. main.cpp  
  2. ------------------------------------------  
  3. #include   
  4.  #include   
  5.  #include   
  6.  #include   
  7.  #include "birthdayparty.h"  
  8.  #include "person.h"  
  9.   
  10.  int main(int argc, char ** argv)  
  11.  {  
  12.      QApplication app(argc, argv);  
  13.   
  14.      qmlRegisterType("People", 1,0, "BirthdayParty");  
  15.      qmlRegisterType("People", 1,0, "Person");  
  16.   
  17.      QDeclarativeEngine engine;  
  18.      QDeclarativeComponent component(&engine, QUrl("qrc:/example.qml"));  
  19.      BirthdayParty *party = qobject_cast(component.create());  
  20.   
  21.      if (party && party->host()) {  
  22.          qWarning() << party->host()->name() << "is having a birthday!";  
  23.          qWarning() << "They are inviting:";  
  24.          for (int i = 0; i < party->guestCount(); ++i)  
  25.              qWarning() << "   " << party->guest(i)->name() << party->guest(i)->shoeSize();  
  26.      } else {  
  27.          qWarning() << component.errors();  
  28.      }  
  29.   
  30.      return app.exec();  
  31.  }  

 

 

     

  1. example.qml  
  2. -----------------------------------------------  
  3. import People 1.0  
  4.   
  5. BirthdayParty {  
  6.     host : Person {  
  7.         name: "Lavy Liu"  
  8.         shoeSize: 12  
  9.     }  
  10.     guests: [  
  11.         Person { name: "Sglaze";  shoeSize: 18},  
  12.         Person { name: "liuhongwei"; shoeSize: 23},  
  13.         Person { name: "liuhw"}  
  14.     ]  
  15. }  

     输出:

     "Lavy Liu" is having a birthday!

   They are inviting:

  "Sglaze" 18

  "liuhongwei" 23

  "liuhw" 0

 

 接下来的代码是增加两个Person的子类,Boy和Girl,并分别注册到QML.

  

    person.h

   

  1. #ifndef PERSON_H  
  2. #define PERSON_H  
  3.   
  4. #include   
  5.   
  6. class Person : public QObject  
  7. {  
  8.     Q_OBJECT  
  9.     //声明属性,用于在QML组件中直接访问  
  10.     Q_PROPERTY(QString name READ name WRITE setName)  
  11.     Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)  
  12.   
  13. public:  
  14.     Person(QObject *parent = 0);  
  15.     QString name() const;  
  16.     void setName(const QString &);  
  17.     int shoeSize() const;  
  18.     void setShoeSize(int);  
  19.   
  20. private:  
  21.     QString m_name;  
  22.     int m_shoeSize;  
  23. };  
  24.   
  25. class Boy : public Person  
  26. {  
  27.     Q_OBJECT  
  28. public:  
  29.     Boy(QObject * parent = 0);  
  30. };  
  31.   
  32. class Girl : public Person  
  33. {  
  34.     Q_OBJECT  
  35. public:  
  36.     Girl(QObject * parent = 0);  
  37. };  
  38.   
  39. #endif // PERSON_H  
  

 

   person.cpp

   

[c-sharp] view plaincopy
  1. #include "person.h"  
  2.   
  3. Person::Person(QObject *parent) : QObject(parent), m_shoeSize(0)  
  4. {  
  5. }  
  6.   
  7. void Person::setName(const QString & name)  
  8. {  
  9.     m_name = name;  
  10. }  
  11.   
  12. QString Person::name() const  
  13. {  
  14.     return m_name;  
  15. }  
  16.   
  17. void Person::setShoeSize(int size)  
  18. {  
  19.     m_shoeSize = size;  
  20. }  
  21.   
  22. int Person::shoeSize() const {  
  23.     return m_shoeSize;  
  24. }  
  25.   
  26. Boy::Boy(QObject *parent) : Person(parent)  
  27. {  
  28.   
  29. }  
  30.   
  31. Girl::Girl(QObject *parent) : Person(parent)  
  32. {  
  33.   
  34. }  

 

   birthdayparty.h

  

  1. #ifndef BIRTHDAYPARTY_H  
  2. #define BIRTHDAYPARTY_H  
  3. #include   
  4. #include   
  5. #include "person.h"  
  6.   
  7. class BirthdayParty : public QObject  
  8. {  
  9.     Q_OBJECT  
  10.     //声明属性,用于在QML组件中直接访问  
  11.     Q_PROPERTY(Person *host READ host WRITE setHost)  
  12.     Q_PROPERTY(QDeclarativeListProperty guests READ guests)  
  13.   
  14. public:  
  15.     BirthdayParty(QObject *parent = 0);  
  16.     Person *host() const;  
  17.     void setHost(Person *);  
  18.   
  19.     QDeclarativeListProperty guests();  
  20.     int guestCount() const;  
  21.     Person *guest(intconst;  
  22. private:  
  23.     Person * m_host;  
  24.     QList m_guests;  
  25. };  
  26.   
  27. #endif // BIRTHDAYPARTY_H  

 

   birthdayparty.cpp

  

[c-sharp] view plaincopy
  1. #include "birthdayparty.h"  
  2.   
  3. BirthdayParty::BirthdayParty(QObject *parent) : QObject(parent),m_host(0)  
  4. {  
  5. }  
  6.   
  7. Person *BirthdayParty::host() const  
  8. {  
  9.     return m_host;  
  10. }  
  11.   
  12. void BirthdayParty::setHost(Person *c) {  
  13.     m_host = c;  
  14. }  
  15.   
  16. QDeclarativeListProperty BirthdayParty::guests()  
  17. {  
  18.     return QDeclarativeListProperty(this, m_guests);  
  19. }  
  20.   
  21. int BirthdayParty::guestCount() const  
  22. {  
  23.     return m_guests.count();  
  24. }  
  25.   
  26. Person *BirthdayParty::guest(int index) const  
  27. {  
  28.    return m_guests.at(index);  
  29. }  

 

   main.cpp

  

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include "birthdayparty.h"  
  6. #include "person.h"  
  7.   
  8. int main(int argc, char *argv[])  
  9. {  
  10.     QApplication a(argc, argv);  
  11.   
  12.     //向QML注册BirthdayParty,Person,Boy,Girl  
  13.     qmlRegisterType("People", 1, 0, "BirthdayParty");  
  14.     qmlRegisterType();  
  15.     qmlRegisterType("People", 1, 0, "Boy");  
  16.     qmlRegisterType("People", 1, 0, "Girl");  
  17.   
  18.     QDeclarativeEngine engine;  
  19.     QDeclarativeComponent component(&engine, QUrl("qrc:/example.qml"));  
  20.     //创建BirthdayParty的QML组件并强转为C++的BirthdayParty类型  
  21.     BirthdayParty *party = qobject_cast(component.create());  
  22.     if(party && party->host())  
  23.     {  
  24.         qWarning() << party->host()->name() <<"is having a birthday!";  
  25.         if(qobject_cast(party->host()))  
  26.             qWarning() << "He is inviting:";  
  27.         else  
  28.             qWarning() << "She is inviting:";  
  29.         for(int i = 0; i < party->guestCount(); ++i)  
  30.         {  
  31.             qWarning() << "  " << party->guest(i)->name();  
  32.         }  
  33.     } else  
  34.     {  
  35.         qWarning() << component.errors();  
  36.     }  
  37.     return a.exec();  
  38. }  

 

   example.qml

  

  1. import People 1.0  
  2.   
  3. BirthdayParty {  
  4.     host : Boy {  
  5.         name: "Lavy Liu"  
  6.         shoeSize: 12  
  7.     }  
  8.     guests: [  
  9.         Boy { name: "Sglaze";  shoeSize: 18},  
  10.         Girl { name: "Lucy"; shoeSize: 20},  
  11.         Girl { name: "Lily"}  
  12.     ]  
  13. }  

 

   输出:

   "Lavy Liu" is having a birthday!

  He is inviting:

 "Sglaze"

 "Lucy" 

 "Lily"

 

 




阅读(2899) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~