diff options
Diffstat (limited to 'examples/declarative/tutorials/extending/chapter2-methods')
-rw-r--r-- | examples/declarative/tutorials/extending/chapter2-methods/app.qml | 24 | ||||
-rw-r--r-- | examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.pro | 4 | ||||
-rw-r--r-- | examples/declarative/tutorials/extending/chapter2-methods/main.cpp | 4 | ||||
-rw-r--r-- | examples/declarative/tutorials/extending/chapter2-methods/piechart.cpp (renamed from examples/declarative/tutorials/extending/chapter2-methods/musician.cpp) | 37 | ||||
-rw-r--r-- | examples/declarative/tutorials/extending/chapter2-methods/piechart.h (renamed from examples/declarative/tutorials/extending/chapter2-methods/musician.h) | 25 |
5 files changed, 58 insertions, 36 deletions
diff --git a/examples/declarative/tutorials/extending/chapter2-methods/app.qml b/examples/declarative/tutorials/extending/chapter2-methods/app.qml index 02d33c2..0b55f7c 100644 --- a/examples/declarative/tutorials/extending/chapter2-methods/app.qml +++ b/examples/declarative/tutorials/extending/chapter2-methods/app.qml @@ -38,23 +38,29 @@ ** ****************************************************************************/ //![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: aMusician - name: "Reddy the Rocker" - instrument: "Guitar" + PieChart { + id: aPieChart + anchors.centerIn: parent + width: 100; height: 100 + color: "red" - onPerformanceEnded: console.log("The performance has now ended") + onChartCleared: console.log("The chart has been cleared") } MouseArea { anchors.fill: parent - onClicked: aMusician.perform() + onClicked: aPieChart.clearChart() + } + + Text { + anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 } + text: "Click anywhere to clear the chart" } } //![0] diff --git a/examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.pro b/examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.pro index bd05ebe..0f04167 100644 --- a/examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.pro +++ b/examples/declarative/tutorials/extending/chapter2-methods/chapter2-methods.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/chapter2-methods/main.cpp b/examples/declarative/tutorials/extending/chapter2-methods/main.cpp index 8ef6965..a5dbab3 100644 --- a/examples/declarative/tutorials/extending/chapter2-methods/main.cpp +++ b/examples/declarative/tutorials/extending/chapter2-methods/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/chapter2-methods/musician.cpp b/examples/declarative/tutorials/extending/chapter2-methods/piechart.cpp index 0ce0022..11ff7c8 100644 --- a/examples/declarative/tutorials/extending/chapter2-methods/musician.cpp +++ b/examples/declarative/tutorials/extending/chapter2-methods/piechart.cpp @@ -37,38 +37,51 @@ ** $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; } -void Musician::setInstrument(const QString &instrument) +void PieChart::setColor(const QColor &color) { - m_instrument = instrument; + m_color = color; +} + +void PieChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + QPen pen(m_color, 2); + painter->setPen(pen); + painter->setRenderHints(QPainter::Antialiasing, true); + painter->drawPie(boundingRect(), 90 * 16, 290 * 16); } //![0] -void Musician::perform() +void PieChart::clearChart() { - qWarning() << m_name << "is playing the" << m_instrument; - emit performanceEnded(); + setColor(QColor(Qt::transparent)); + update(); + + emit chartCleared(); } //![0] diff --git a/examples/declarative/tutorials/extending/chapter2-methods/musician.h b/examples/declarative/tutorials/extending/chapter2-methods/piechart.h index 86849ba..246fd9f 100644 --- a/examples/declarative/tutorials/extending/chapter2-methods/musician.h +++ b/examples/declarative/tutorials/extending/chapter2-methods/piechart.h @@ -37,41 +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) + Q_PROPERTY(QColor color READ color WRITE setColor) //![1] 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); + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); //![2] - Q_INVOKABLE void perform(); + Q_INVOKABLE void clearChart(); signals: - void performanceEnded(); + void chartCleared(); //![2] private: QString m_name; - QString m_instrument; + QColor m_color; //![3] }; |