Qt5 official demo release set 28 -- extending qml-signal support example

Source: Internet
Author: User

All articles in this series can be viewed here in http://blog.csdn.net/cloud_castle/article/category/2123873

Link to qt5 official demo release set 27 -- extending qml-Attached properties example


This demo demonstrates how to add signals for the qml custom type, which is similar to what qt5 official demo consumer set 16 -- Chapter 2: connecting to C ++ methods and signals introduces, given the size of the example, the article may be clearer.


In any case, we still need to extend this birthdayparty routine. We added a "party start" signal for this birthday party and defined a function to send this signal.

Since the person class remains unchanged, let's look at the changed birthdayparty. h:

# Ifndef birthdayparty_h # define birthdayparty_h # include <qobject> # include <qdate> # include <qqml. h> # include "person. H "class birthdaypartyattached: Public qobject {q_object q_property (qdate RSVP read RSVP write setrsvp) Public: birthdaypartyattached (qobject * object); qdate RSVP () const; void setrsvp (const qdate &); Private: qdate m_rsvp;}; Class birthdayparty: Public qobject {q_object q_property (person * Host read host write sethost) q_property (qqmllistproperty <person> guests read guests) q_classinfo ("defaultproperty", "guests") Public: birthdayparty (qobject * parent = 0 ); person * Host () const; void sethost (person *); qqmllistproperty <person> guests (); int guestcount () const; person * guest (INT) const; static birthdaypartyattached * qmlattachedproperties (qobject *); void startparty (); // custom function. Q_invokable is used because the function is called in Main. cpp instead of qml //! [0] signals: void partystarted (const qtime & time); // custom signal //! [0] private: person * m_host; qlist <person *> m_guests;}; qml_declare_typeinfo (birthdayparty, qml_has_attached_properties) # endif // birthdayparty_h


Birthdayparty. cpp:

# Include "birthdayparty. H "birthdaypartyattached: birthdaypartyattached (qobject * object): qobject (object) {} qdate response: RSVP () const {return m_rsvp;} void birthdaypartyattached :: setrsvp (const qdate & D) {m_rsvp = D;} birthdayparty: birthdayparty (qobject * parent): qobject (parent), m_host (0) {} person * birthdayparty :: host () const {return m_host;} void birthdayparty: sethost (person * c) {m_host = C;} qqmllistproperty <person> birthdayparty: Guests () {return qqmllistproperty <person> (this, m_guests);} int birthdayparty: guestcount () const {return m_guests.count ();} person * birthdayparty: Guest (INT index) const {return m_guests.at (INDEX);} void birthdayparty: startparty () // This function is used to transmit the current time as a signal parameter {qtime time = qtime: currenttime (); emit partystarted (time);} birthdaypartyattached * birthdayparty: qmlattachedproperties (qobject * object) {return New birthdaypartyattached (object );}

Example. qml:

Import people 1.0 import qtquick 2.0 // For qcolorbirthdayparty {//! [0] onpartystarted: console. Log ("this party started rockin 'At" + time); // In qml, we do not need to process the signal //! [0] // you only need to pay attention to the onxxx function corresponding to the signal. // when the signal is sent, the function is executed HOST: Boy {name: "Bob Jones" shoe {size: 12; color: "white"; Brand: "Nike"; Price: 90.0} Boy {name: "Leo Hodge" birthdayparty. RSVP: "2009-07-06" shoe {size: 10; color: "black"; Brand: "Reebok"; Price: 59.95} Boy {name: "Jack Smith" shoe {size: 8; color: "blue"; Brand: "Puma"; Price: 19.95} girl {name: "Anne Brown" birthdayparty. RSVP: "200 9-07-01 "shoe. Size: 7 shoe. Color:" red "shoe. Brand:" Marc jacbs "shoe. Price: 699.99 }//! [1]} //! [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 <birthdaypartyattached> (); qmlregistertype <birthdayparty> (" people ", 1,0, "birthdayparty"); qmlregistertype <shoedescription> (); qmlregistertype <person> (); qmlregistertype <boy> ("people ",, "Boy"); qmlregistertype <girl> ("people", "girl"); qqmlengine engine; qqmlcomponent component (& 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 () qwarning () <" He is inviting: "; else qwarning () <"she is inviting:"; for (int ii = 0; II <party-> guestcount (); ++ II) {person * guest = party-> guest (II); qdate rsvpdate; qobject * attached = qmlattachedpropertiesobject <birthdayparty> (guest, false); If (attached) rsvpdate = attached-> property ("RSVP "). todate (); If (rsvpdate. isnull () qwarning () <"" <guest-> name () <"RSVP date: hasn't RSVP 'd"; else qwarning () <"" <guest-> name () <"RSVP date:" <qprintable (rsvpdate. tostring (); // qprintable is discussed above} party-> startparty (); // The transmit signal function is called here, therefore, the start time of the party will be printed on the last line} else {qwarning () <component. errors ();} return 0 ;}

Result


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.