Qt5 official demo release set 18 -- Chapter 4: using custom property types

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 17 -- Chapter 3: Adding property bindings


In the previous "Pie Chart" demo, we defined the "name" and "color" attributes for this custom type. They are based on the qt built-in types "qstring" and "qcolor ", this example shows how to add custom attributes for this piechart. Using custom type attributes makes it easier for us to implement Modular programming.

This project has a pieslice class, which is used to draw a pie chart, while piechart is only used to provide the framework:



Piechart. h:

# Ifndef piechart_h # define piechart_h # include <qtquick/qquickitem> class pieslice ;//! [0] class piechart: Public qquickitem // because this class no longer needs to be drawn, You can inherit from qquickitem {q_object q_property (pieslice * pieslice read pieslice write setpieslice) // We have defined the pieslice attribute. It is a pieslice class pointer //! [0] q_property (qstring name read name write setname) // The color attribute is moved to pieslice //! [1] public ://! [1] piechart (qquickitem * parent = 0); qstring name () const; void setname (const qstring & name );//! [2] pieslice * pieslice () const; void setpieslice (pieslice * pieslice );//! [2] private: qstring m_name; pieslice * m_pieslice ;//! [3]}; //! [3] # endif

Piechart. cpp:

# Include "piechart. H "# include" pieslice. H "piechart: piechart (qquickitem * parent): qquickitem (parent) {}qstring piechart: Name () const {return m_name;} void piechart :: setname (const qstring & name) {m_name = Name;} pieslice * piechart: pieslice () const {return m_pieslice ;}//! [0] void piechart: setpieslice (pieslice * pieslice) {m_pieslice = pieslice; pieslice-> setparentitem (this); // A qml visualization component must have a parent object, otherwise it cannot be displayed }//! [0]


Place the painting in a separate class, pieslice. h:

# Ifndef pieslice_h # define pieslice_h # include <qtquick/qquickpainteditem> # include <qcolor> //! [0] class pieslice: Public qquickpainteditem // inherit this class to overload paint {q_object q_property (qcolor color read color write setcolor) Public: pieslice (qquickitem * parent = 0 ); qcolor color () const; void setcolor (const qcolor & color); void paint (qpainter * painter); Private: qcolor m_color ;};//! [0] # endif

Pieslice. cpp:

#include "pieslice.h"#include <QPainter>PieSlice::PieSlice(QQuickItem *parent)    : QQuickPaintedItem(parent){}QColor PieSlice::color() const{    return m_color;}void PieSlice::setColor(const QColor &color){    m_color = color;}void PieSlice::paint(QPainter *painter){    QPen pen(m_color, 2);    painter->setPen(pen);    painter->setRenderHints(QPainter::Antialiasing, true);    painter->drawPie(boundingRect().adjusted(1, 1, -1, -1), 90 * 16, 290 * 16);}

Main. cpp:

# Include "piechart. H" # include "pieslice. H" # include <qtquick/qquickview> # include <qguiapplication> //! [0] int main (INT argc, char * argv []) {//! [0] qguiapplication app (argc, argv); qmlregistertype <piechart> ("Charts", 1, 0, "piechart"); // register these two C ++ classes, defined in a namespace //! [1] qmlregistertype <pieslice> ("Charts", 1, 0, "pieslice ");//! [1] qquickview view; view. setresizemode (qquickview: sizerootobjecttoview); view. setsource (qurl ("qrc: // app. qml "); view. show (); Return app.exe C ();//! [2]} //! [2]

App. qml:

Import charts 1.0 import qtquick 2.0 item {width: 300; Height: 200 piechart {ID: Chart anchors. centerin: parent width: 100; Height: 100 pieslice: pieslice {// set anchors for the pieslice attribute. fill: parent color: "red"} component. oncompleted: console. log ("the pie is colored" + chart. pieslice. color) // display the RGB value of the pie chart when the component is finished }//! [0]


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.