Qt5 official demo release set 21 -- extending qml-adding types example

Source: Internet
Author: User

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


It is a new series, but this series is similar to our previous chapter series. However, chapter mainly demonstrates how to use C ++ to create a type with visibility to expand our qml, and this series focuses on how to use C ++ to expand qml non-visualized content.

The first small example here is similar to the first small example of chapter:


Person is our custom C ++ class, and then we register it as the qml type for example. qml in the resource file.

Person. H. This class is not much different from the previous piechart:

# Ifndef person_h # define person_h # include <qobject> //! [0] class person: Public qobject // note that this object does not need to be visualized, we can inherit the most basic qobject. {q_object // because the qml component is based on the metadata system, neither qobject nor q_object can be less than q_property (qstring name read name write setname) // two Custom Attributes 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]

Person. cpp:

#include "person.h"// ![0]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;}

Example. qml:

Import people 1.0 person {// non-visualized component, and we no longer need to use item as the parent Object Name: "Bob Jones" shoesize: 12}

Main. cpp:

# Include <qcoreapplication> // note that qguiapplication # include <qqmlengine> in chapter provides the running environment of qml components # include <qqmlcomponent> // encapsulate and access qml components # include <qdebug> # include "person. H "int main (INT argc, char ** argv) {qcoreapplication app (argc, argv );//! [0] qmlregistertype <person> ("people", "person"); // register the qml type //! [0] qqmlengine engine; qqmlcomponent (& engine, qurl ("qrc: Example. qml "); // obtain the component person * person = qobject_cast <person *> (component. create (); // create the instantiated object of this component if (person) {qwarning () <"the person's name is" <person-> name (); // still access its member function qwarning () <"they wear a" <person-> shoesize () <"sized shoe ";} else {qwarning () <component. errors (); // if the type conversion fails, the error message is returned.} return 0 ;}


Because there is no interface, the actual operation is the output of a single console:


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.