All articles in this series can be viewed here in http://blog.csdn.net/cloud_castle/article/category/2123873
Link to qt5 official demo example set 22 -- extending qml-object and list property types example
In the previous example, we created a property guests with a list parameter for the birthdayparty class, and the type of this list parameter is consistent, that is, person. However, using the qml type conversion mechanism, we can make the type of this list parameter different. Now, let's take a look at example. qml:
Import people 1.0 //! [0] birthdayparty {Host: Boy {name: "Bob Jones" shoesize: 12} guests: [// to know the registration attribute, we need to give it a fixed parameter type, for example, Int, bool, or even user-defined type boy {name: "Leo Hodges"}, // to get the effect of this code, we need to use inheritance boy {Name: "Jack Smith"}, // both boy and girl inherit from person, and registering guests as person is enough for girl {name: "Anne Brown"}]}/! [0]
Since the birthdayparty class code has not changed, let's look at person. h:
# Ifndef person_h # define person_h # include <qobject> class person: Public qobject // person has not been changed {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 ;};//! [0] class boy: public person // create a boy inherited from person {q_objectpublic: Boy (qobject * parent = 0 ); // a basic constructor is required so that qml can instantiate this object };//! [Girl class] class girl: public person // girl is the same as boy {q_objectpublic: Girl (qobject * parent = 0 );};//! [Girl class] //! [0] # 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 ;}//! [1] BOY: Boy (qobject * parent) // because this example only demonstrates inheritance, no additional function is added for the derived class: Person (parent) {} girl:: Girl (qobject * parent) // constructor is required: Person (parent ){}//! [1]
Main. cpp:
# Include <qcoreapplication> # include <qqmlengine> # include <qqmlcomponent> # include <qdebug> # include "birthdayparty. H "# include" person. H "int main (INT argc, char ** argv) {qcoreapplication app (argc, argv); qmlregistertype <birthdayparty> (" people ", 1, 0," birthdayparty "); //! [0] qmlregistertype <person> (); // we still need to register person; otherwise, the boy and girl cannot be forcibly converted to person //! [0] // This function parameter is null, because we do not need to instantiate a person object //! [Register boy girl] qmlregistertype <boy> ("people", "boy"); // register boy and girl qmlregistertype <girl> ("people, "Girl ");//! [Register boy girl] qqmlengine engine; qqmlcomponent (& engine, qurl ("qrc: Example. qml "); birthdayparty * party = qobject_cast <birthdayparty *> (component. create (); If (party & party-> host () {qwarning () <party-> host ()-> name () <"is having a birthday! "; If (qobject_cast <boy *> (Party-> host () // determines whether the host is a boy or a girl qwarning () <" He is inviting :"; else qwarning () <"she is inviting:"; for (int ii = 0; II <party-> guestcount (); ++ II) qwarning () <"" <party-> guest (II)-> name ();} else {qwarning () <component. errors ();} return 0 ;}