Qt5官方demo解析集18——Chapter 4: Using Custom Property Types

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873

接上文Qt5官方demo解析集17——Chapter
3: Adding Property Bindings

在前面的“饼状图”Demo中,我们为这个自定义类型定义了"name"和"color"属性,他们都是基于Qt内置类型"QString"和"QColor",这个例子则向我们演示了如何为这个PieChart添加自定义类型的属性。使用自定义类型的属性,更方便我们模块化编程。

这个项目中多了一个pieslice类,它用于饼状图的实际绘制,而PieChart仅用来提供框架:

piechart.h:

#ifndef PIECHART_H
#define PIECHART_H

#include <QtQuick/QQuickItem>

class PieSlice;

//![0]
class PieChart : public QQuickItem             // 由于这个类不再需要进行实际绘制,所以继承自QQuickItem就可以了
{
    Q_OBJECT
    Q_PROPERTY(PieSlice* pieSlice READ pieSlice WRITE setPieSlice)  // 我们定义了pieSlice属性,它是一个PieSlice类的指针
//![0]
    Q_PROPERTY(QString name READ name WRITE setName)         // color属性被移至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);              // QML的可视化组件必须拥有一个父对象,否则无法显示
}
//![0]

将具体绘制放在一个单独的类中,pieslice.h:

#ifndef PIESLICE_H
#define PIESLICE_H

#include <QtQuick/QQuickPaintedItem>
#include <QColor>

//![0]
class PieSlice : public QQuickPaintedItem       // 继承这个类以重载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");  // 注册这两个C++类,定义在一个命名空间中

//![1]
    qmlRegisterType<PieSlice>("Charts", 1, 0, "PieSlice");
//![1]

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setSource(QUrl("qrc:///app.qml"));
    view.show();
    return app.exec();

//![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 {             // PieSlice属性的设置
            anchors.fill: parent
            color: "red"
        }
    }

    Component.onCompleted: console.log("The pie is colored " + chart.pieSlice.color)  // 最后在组件完成时显示这个饼状图的RGB值
}
//![0]

Qt5官方demo解析集18——Chapter 4: Using Custom Property Types,布布扣,bubuko.com

时间: 2024-08-25 00:46:06

Qt5官方demo解析集18——Chapter 4: Using Custom Property Types的相关文章

Qt5官方demo解析集19——Chapter 5: Using List Property Types

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集18--Chapter 4: Using Custom Property Types 上个例子向我们展示了如何为QML调用的C++类型添加自定义类型的属性,在这个例子中我们更进一步,将这个类型更换为一个PieSlice的列表,以此得到更丰富的处理能力. 项目文件与上个例子是一样的. 还是先看看piechart.h: #if

Qt5官方demo解析集20——Chapter 6: Writing an Extension Plugin

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集19--Chapter 5: Using List Property Types 在前文中我们定义的PieChart和PieSlice这两个自定义QML类型只用来供app.qml文件使用,如果希望我们所定义的类型可以被多个qml使用,那么可以将其创建为可扩展的插件.由于是将C++定义的类创建为供QML使用的插件,所以这与我

Qt5官方demo解析集15——Chapter 1: Creating a New Type

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 前面我们说到了QML的粒子系统,它可以创造丰富多彩的粒子特效.但是更多的情况下,我们的QML界面是配合C++进行工作的:QML负责界面渲染,C++负责逻辑事务.甚至有时,我们还会利用QML来绘制C++代码中定义的可视化组件,或者使用C++代码来访问QML中对象的属性等.从这篇博文开始,我们介绍了Qt官方Demo中的"Chapter"系列,它介

Qt5官方demo解析集17——Chapter 3: Adding Property Bindings

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集16--Chapter 2: Connecting to C++ Methods and Signals 在C++中我们通常将用户的交互与处理函数用信号槽绑定起来,比如窗口尺寸的变化,颜色的变化等,但在QML中,我们更多的使用属性绑定来完成这些功能.我们可以将这个属性值绑定到另一个对象或者本身的属性值上,这样当后者改变时,

Qt5官方demo解析集16——Chapter 2: Connecting to C++ Methods and Signals

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集15--Chapter 1: Creating a New Type 在上篇博文我们了解到如何在C++代码中将一个C++类注册为一个QML类型,并供QML文件使用.接下来这个Demo中进一步向这个PieChart中添加信号和方法供QML使用. 在项目上没有什么改变,我们直接来看代码PieChart.h: #ifndef P

Qt5官方demo解析集22——Extending QML - Object and List Property Types Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集21--Extending QML - Adding Types Example 在上一个例子中我们基于C++创建了一个自定义的QML类型,接下来,我们将该类作为另一个类的属性类型,定义了另一个birthdayparty类.这个例子与Qt5官方demo解析集19--Chapter 5: Using List Propert

Qt5官方demo解析集30——Extending QML - Binding Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集29--Extending QML - Property Value Source Example 还记得我们曾经在Qt5官方demo解析集17--Chapter 3: Adding Property Bindings一文中接触过QML自定义类型的属性绑定吗?如果不记得了,可以移步进行了解.因为项目尺寸的原因,那个例子可能

Qt5官方demo解析集28——Extending QML - Signal Support Example

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集27--Extending QML - Attached Properties Example 这个demo演示了为QML自定义类型添加信号的方法,这与Qt5官方demo解析集16--Chapter 2: Connecting to C++ Methods and Signals所介绍的差不多,鉴于例子的尺寸,可能那一篇要

Qt5官方demo解析集31——StocQt

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集30--Extending QML - Binding Example 最近在做QML制表,因此想找一些相关曲线绘制的demo看看,结果发现了这个例子,觉得挺不错,它相比于我们之前的Extend和Particle系列显得更大一些,涉及到的面也更广一些.因此想拿过来给大家分享~ 这个例子是基于QML的股票走势图绘制,数据来源