diff options
Diffstat (limited to 'examples/declarative/tutorials/extending/chapter3-bindings')
-rw-r--r-- | examples/declarative/tutorials/extending/chapter3-bindings/app.qml | 42 | ||||
-rw-r--r-- | examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro | 4 | ||||
-rw-r--r-- | examples/declarative/tutorials/extending/chapter3-bindings/main.cpp | 4 | ||||
-rw-r--r-- | examples/declarative/tutorials/extending/chapter3-bindings/piechart.cpp (renamed from examples/declarative/tutorials/extending/chapter3-bindings/musician.cpp) | 39 | ||||
-rw-r--r-- | examples/declarative/tutorials/extending/chapter3-bindings/piechart.h (renamed from examples/declarative/tutorials/extending/chapter3-bindings/musician.h) | 26 |
5 files changed, 66 insertions, 49 deletions
diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/app.qml b/examples/declarative/tutorials/extending/chapter3-bindings/app.qml index 8bf6ecf..2ff6ae1 100644 --- a/examples/declarative/tutorials/extending/chapter3-bindings/app.qml +++ b/examples/declarative/tutorials/extending/chapter3-bindings/app.qml @@ -38,35 +38,37 @@ ** ****************************************************************************/ //![0] -import Music 1.0 +import Charts 1.0 import Qt 4.7 -Rectangle { - width: 200; height: 200 +Item { + width: 300; height: 200 - Musician { - id: reddy - name: "Reddy the Rocker" - instrument: "Guitar" - } + Row { + anchors.centerIn: parent + spacing: 20 + + PieChart { + id: chartA + width: 100; height: 100 + color: "red" + } - Musician { - id: craig - name: "Craig the Copycat" - instrument: reddy.instrument + PieChart { + id: chartB + width: 100; height: 100 + color: chartA.color + } } MouseArea { anchors.fill: parent - onClicked: { - reddy.perform() - craig.perform() - - reddy.instrument = "Drums" + onClicked: { chartA.color = "blue" } + } - reddy.perform() - craig.perform() - } + Text { + anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 } + text: "Click anywhere to change the chart color" } } //![0] diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro b/examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro index bd05ebe..0f04167 100644 --- a/examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro +++ b/examples/declarative/tutorials/extending/chapter3-bindings/chapter3-bindings.pro @@ -1,5 +1,5 @@ QT += declarative -HEADERS += musician.h -SOURCES += musician.cpp \ +HEADERS += piechart.h +SOURCES += piechart.cpp \ main.cpp diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/main.cpp b/examples/declarative/tutorials/extending/chapter3-bindings/main.cpp index 8ef6965..a5dbab3 100644 --- a/examples/declarative/tutorials/extending/chapter3-bindings/main.cpp +++ b/examples/declarative/tutorials/extending/chapter3-bindings/main.cpp @@ -38,7 +38,7 @@ ** ****************************************************************************/ //![0] -#include "musician.h" +#include "piechart.h" #include <qdeclarative.h> #include <QDeclarativeView> #include <QApplication> @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); - qmlRegisterType<Musician>("Music", 1, 0, "Musician"); + qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart"); QDeclarativeView view; view.setSource(QUrl::fromLocalFile("app.qml")); diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/musician.cpp b/examples/declarative/tutorials/extending/chapter3-bindings/piechart.cpp index 5f9ead4..85a9762 100644 --- a/examples/declarative/tutorials/extending/chapter3-bindings/musician.cpp +++ b/examples/declarative/tutorials/extending/chapter3-bindings/piechart.cpp @@ -37,40 +37,53 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "musician.h" +#include "piechart.h" +#include <QPainter> #include <QDebug> -Musician::Musician(QObject *parent) - : QObject(parent) +PieChart::PieChart(QDeclarativeItem *parent) + : QDeclarativeItem(parent) { + // need to disable this flag to draw inside a QDeclarativeItem + setFlag(QGraphicsItem::ItemHasNoContents, false); } -QString Musician::name() const +QString PieChart::name() const { return m_name; } -void Musician::setName(const QString &name) +void PieChart::setName(const QString &name) { m_name = name; } -QString Musician::instrument() const +QColor PieChart::color() const { - return m_instrument; + return m_color; } //![0] -void Musician::setInstrument(const QString &instrument) +void PieChart::setColor(const QColor &color) { - if (instrument != m_instrument) { - m_instrument = instrument; - emit instrumentChanged(); + if (color != m_color) { + m_color = color; + update(); // repaint with the new color + emit colorChanged(); } } //![0] -void Musician::perform() +void PieChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { - qWarning() << m_name << "is playing the" << m_instrument; + QPen pen(m_color, 2); + painter->setPen(pen); + painter->setRenderHints(QPainter::Antialiasing, true); + painter->drawPie(boundingRect(), 90 * 16, 290 * 16); +} + +void PieChart::clearChart() +{ + setColor(QColor(Qt::transparent)); + update(); } diff --git a/examples/declarative/tutorials/extending/chapter3-bindings/musician.h b/examples/declarative/tutorials/extending/chapter3-bindings/piechart.h index 0b0addb..164cebb 100644 --- a/examples/declarative/tutorials/extending/chapter3-bindings/musician.h +++ b/examples/declarative/tutorials/extending/chapter3-bindings/piechart.h @@ -37,42 +37,44 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#ifndef MUSICIAN_H -#define MUSICIAN_H +#ifndef PIECHART_H +#define PIECHART_H -#include <QObject> +#include <QDeclarativeItem> +#include <QColor> //![0] -class Musician : public QObject +class PieChart : public QDeclarativeItem { //![0] Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(QString instrument READ instrument WRITE setInstrument) //![1] - Q_PROPERTY(QString instrument READ instrument WRITE setInstrument NOTIFY instrumentChanged) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) public: //![1] - Musician(QObject *parent = 0); + PieChart(QDeclarativeItem *parent = 0); QString name() const; void setName(const QString &name); - QString instrument() const; - void setInstrument(const QString &instrument); + QColor color() const; + void setColor(const QColor &color); - Q_INVOKABLE void perform(); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + + Q_INVOKABLE void clearChart(); //![2] signals: - void instrumentChanged(); + void colorChanged(); //![2] private: QString m_name; - QString m_instrument; + QColor m_color; //![3] }; |