这里显示的是几个交通灯变化的画面。它们也同时对应我们现实生活中的交通灯变化的几个状态。
在这篇文章里,我们将学到如下的东西:
#ifndef TRAFFICLIGHT_H #define TRAFFICLIGHT_H #include <QtQuick/QQuickPaintedItem> #include <QColor> class TrafficLight : public QQuickPaintedItem { Q_OBJECT Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) public: explicit TrafficLight(QQuickItem *parent = 0); void paint(QPainter *painter); QColor color() const; void setColor(const QColor &color); signals: void colorChanged(); public slots: private: QColor m_color; }; #endif // TRAFFICLIGHT_H
<pre style="margin-top: 0px; margin-bottom: 0px;"><!--StartFragment--><span style=" color:#000080;"></span>
#include <QPainter>
#include <QRadialGradient>
#include "trafficlight.h"
TrafficLight::TrafficLight(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
}
QColor TrafficLight::color() const
{
return m_color;
}
void TrafficLight::setColor(const QColor &color)
{
if ( color == m_color )
return;
else {
m_color = color;
update(); // repaint with the new color
emit colorChanged();
}
}
void TrafficLight::paint(QPainter *painter)
{
QRectF rect(boundingRect());
QPen pen;
pen.setWidthF( 0 );
pen.setColor(m_color);
painter->setPen( pen );
QRadialGradient g( rect.width()/2, rect.height()/2,
rect.width()/2, rect.width()/2, height()/2 );
g.setColorAt( 0.0, Qt::white );
g.setColorAt( 1.0, m_color );
painter->setBrush( g );
painter->drawEllipse(rect);
}这里我们定义了一个称作为“TrafficLight”的类。根据不同的颜色,用它来画一个“QRadialGradient”。我们可以看到在这个类中我们定义了一个称作“color"的property。
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
set( Lightbackend_SRCS modules/Light/backend.cpp modules/Light/mytype.cpp modules/Light/trafficlight.cpp )
#include <QtQml> #include <QtQml/QQmlContext> #include "backend.h" #include "mytype.h" #include "trafficlight.h" void BackendPlugin::registerTypes(const char *uri) { Q_ASSERT(uri == QLatin1String("Light")); qmlRegisterType<MyType>(uri, 1, 0, "MyType"); qmlRegisterType<TrafficLight>(uri, 1, 0, "TrafficLight"); } void BackendPlugin::initializeEngine(QQmlEngine *engine, const char *uri) { QQmlExtensionPlugin::initializeEngine(engine, uri); }
import QtQuick 2.0 import Ubuntu.Components 0.1 import "ui" import Light 1.0 MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "com.ubuntu.developer.liu-xiao-guo.TrafficLight" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true width: units.gu(100) height: units.gu(75) TrafficLight{ id: redlight width: 100 height: 100 color:"red" } }
import QtQuick 2.0 import Ubuntu.Components 0.1 import "ui" import Light 1.0 MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "com.ubuntu.developer.liu-xiao-guo.TrafficLight" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true width: units.gu(100) height: units.gu(75) Page { id:main anchors.fill: parent property int radius: 155 Image{ anchors.horizontalCenter: parent.horizontalCenter height:parent.height source: "light2.png" fillMode: Image.PreserveAspectFit Column { anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter spacing: 28 TrafficLight{ id: redlight width: main.radius height: main.radius color:"red" } TrafficLight{ id: yellowlight width: main.radius height: main.radius color:"yellow" } TrafficLight{ id: greenlight width: main.radius height: main.radius color:"green" } } } } }
states: [ State { name: "red_on" PropertyChanges { target: redlight color:"red" scale: 1.0 } PropertyChanges { target: greenlight color: "black" } PropertyChanges { target: yellowlight color: "black" } }, State { name: "red_yellow_on" PropertyChanges { target: redlight color: "red" } PropertyChanges { target: yellowlight color: "yellow" } PropertyChanges { target: greenlight color: "black" } }, State { name: "green_on" PropertyChanges { target: redlight color: "black" } PropertyChanges { target: yellowlight color: "black" } PropertyChanges { target: greenlight color: "green" } }, State { name: "yellow_on" PropertyChanges { target: redlight color: "black" } PropertyChanges { target: yellowlight color: "yellow" } PropertyChanges { target: greenlight color: "black" } } ]
transitions: [ Transition { from: "*" to: "*" PropertyAnimation { target: redlight properties: "scale, color" duration: 1000 easing.type: Easing.InQuad } PropertyAnimation { target: greenlight properties: "scale, color" duration: 1000 easing.type: Easing.InQuad } PropertyAnimation { target: yellowlight properties: "scale, color" duration: 1000 easing.type: Easing.InQuad } } ]
Timer { interval: 1000 running: true repeat: true property int count: 0 onTriggered: { if (parent.state == "red_on" && count >= 5) { parent.state = "red_yellow_on" count = 0 } else if ( parent.state == "red_yellow_on" ) { parent.state = "green_on" count++ } else if ( parent.state == "green_on" && count >= 5 ) { parent.state = "yellow_on" count ++ } else if ( parent.state == "yellow_on" ) { parent.state = "red_on" count = 0 } else { count++ } } }
import QtQuick 2.0 import Ubuntu.Components 0.1 import "ui" MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "com.ubuntu.developer.liu-xiao-guo.TrafficLight" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true width: units.gu(120) height: units.gu(80) Page { id:main anchors.fill: parent Row { id: myrow anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter spacing: units.gu(5) MyLight { id:light1 width: main.width/5 height: width*3 } MyLight { id:light2 width: main.width/5 height: width*3 } MyLight { id:light3 width: main.width/5 height: width*3 } } } }
import QtQuick 2.0 import Ubuntu.Components 0.1 import Light 1.0 Item { width: units.gu(100) height: units.gu(75) Rectangle { id: background anchors.fill: parent color: "black" property int size: width*0.7 Column { anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter spacing: units.gu(3) TrafficLight{ id: redlight width: background.size height: background.size color:"red" } TrafficLight{ id: yellowlight width: background.size height: background.size color:"yellow" } TrafficLight{ id: greenlight width: background.size height: background.size color:"green" } state: "red_on" states: [ State { name: "red_on" PropertyChanges { target: redlight color:"red" scale: 1.0 } PropertyChanges { target: greenlight color: "black" } PropertyChanges { target: yellowlight color: "black" } }, State { name: "red_yellow_on" PropertyChanges { target: redlight color: "red" } PropertyChanges { target: yellowlight color: "yellow" } PropertyChanges { target: greenlight color: "black" } }, State { name: "green_on" PropertyChanges { target: redlight color: "black" } PropertyChanges { target: yellowlight color: "black" } PropertyChanges { target: greenlight color: "green" } }, State { name: "yellow_on" PropertyChanges { target: redlight color: "black" } PropertyChanges { target: yellowlight color: "yellow" } PropertyChanges { target: greenlight color: "black" } } ] transitions: [ Transition { from: "*" to: "*" PropertyAnimation { target: redlight properties: "scale, color" duration: 1000 easing.type: Easing.InQuad } PropertyAnimation { target: greenlight properties: "scale, color" duration: 1000 easing.type: Easing.InQuad } PropertyAnimation { target: yellowlight properties: "scale, color" duration: 1000 easing.type: Easing.InQuad } } ] Timer { interval: 1000 running: true repeat: true property int count: 0 onTriggered: { if (parent.state == "red_on" && count >= 5) { parent.state = "red_yellow_on" count = 0 } else if ( parent.state == "red_yellow_on" ) { parent.state = "green_on" count++ } else if ( parent.state == "green_on" && count >= 5 ) { parent.state = "yellow_on" count ++ } else if ( parent.state == "yellow_on" ) { parent.state = "red_on" count = 0 } else { count++ } } } } } }