Qt5 official demo example set 25 -- extending qml-methods 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 consumer set 24 -- extending qml-default property example


This example mainly introduces how to define functions in the qml type.

Person. h:

# Ifndef person_h # define person_h # include <qobject> class person: Public qobject // The basic person class is not changed, deleted boy and girl {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;}

Birthdayparty. h:

# Ifndef birthdayparty_h # define birthdayparty_h # include <qobject> # include <qqmllistproperty> # include "person. H "class birthdayparty: Public qobject {q_object q_property (person * Host read host write sethost) q_property (qqmllistproperty <person> guests read guests) Public: birthdayparty (qobject * parent = 0 ); person * Host () const; void sethost (person *); qqmllistproperty <person> guests (); int guestcount ( ) Const; person * guest (INT) const ;//! [0] // a function q_invokable void invite (const qstring & name) is defined here; // to be able to call this function in qml, we need to use the macro q_invokable //! [0] // This macro allows this function to be called through the metadata system // otherwise, the compiler will complain about invite's undefined PRIVATE: person * m_host when calling it in qml; qlist <person *> m_guests;}; # endif // birthdayparty_h

Birthdayparty. cpp:

# Include "birthdayparty. H" birthdayparty: birthdayparty (qobject * parent): qobject (parent), m_host (0 ){}//! [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: Invite (const qstring & name) // function implementation { Person * person = new person (this); person-> setname (name); m_guests.append (person );}//! [0]

Example. qml:

Import qtquick 2.0 import people 1.0 birthdayparty {Host: person {name: "Bob Jones" shoesize: 12} guests: [person {name: "Leo Hodges"}, person {Name: "Jack Smith"}, person {name: "Anne Brown"}] //! [0] component. oncompleted: Invite ("William green") // we need to call this function in a handler //! [0] // This is called when the component is complete}

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");    qmlRegisterType<Person>("People", 1,0, "Person");    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!";        qWarning() << "They are inviting:";        for (int ii = 0; ii < party->guestCount(); ++ii)            qWarning() << "   " << party->guest(ii)->name();    } else {        qWarning() << component.errors();    }    return 0;}

Running result:


We can see that the first three guests exist as the guests attribute value initialization, while William green is "invited" only after the component is loaded.

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.