Qt Quick Extras Examples
阅读官方的源码然后尝试做了下
01 A car dashboard
-
样例演示:
-
-
说明:
ValueSource
组件控制数值相关的动画,例如图中数值的变化;TurnIndicator
组件是控制左右方向灯的闪烁和背景,里面使用了Canvas
来绘制样式;IconGaugeStyle
是对DashboardGaugeStyle
的封装,里面绘制了带图标仪表盘的样式,用于左边油量表和水温表的样式;DashboardGaugeStyle
是对CircularGaugeStyle
的封装,用于中间的速度仪表的样式;TachometerStyle
是对DashboardGaugeStyle
的封装,用于右边转速表的样式;
具体代码:
TestExtrasDashboard.qml
:
import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
import "./Component"
Rectangle {
id: root
width: 1024
height: 600
color: "#161616"
ValueSource {
id: valueSource
}
// Dashboards are typically in a landscape orientation, so we need to ensure
// our height is never greater than our width.
Item {
id: container
width: root.width
height: Math.min(root.width, root.height)
anchors.centerIn: parent
Row {
id: gaugeRow
spacing: container.width * 0.02
anchors.centerIn: parent
TurnIndicator {
id: leftIndicator
anchors.verticalCenter: parent.verticalCenter
width: height
height: container.height * 0.1 - gaugeRow.spacing
direction: Qt.LeftArrow
on: valueSource.turnSignal === Qt.LeftArrow
}
Item {
width: height
height: container.height * 0.25 - gaugeRow.spacing
anchors.verticalCenter: parent.verticalCenter
CircularGauge {
id: fuelGauge
value: valueSource.fuel
maximumValue: 1
y: parent.height / 2 - height / 2 - container.height * 0.01
width: parent.width
height: parent.height * 0.7
style: IconGaugeStyle {
id: fuelGaugeStyle
icon: "qrc:/Icons/Used_Images/Icons/02_10-fuel-icon.png"
minWarningColor: Qt.rgba(0.5, 0, 0, 1)
tickmarkLabel: Text {
color: "white"
visible: styleData.value === 0 || styleData.value === 1
font.pixelSize: fuelGaugeStyle.toPixels(0.225)
text: styleData.value === 0 ? "E" : (styleData.value === 1 ? "F" : "")
}
}
}
CircularGauge {
value: valueSource.temperature
maximumValue: 1
width: parent.width
height: parent.height * 0.7
y: parent.height / 2 + container.height * 0.01
style: IconGaugeStyle {
id: tempGaugeStyle
icon: "qrc:/Icons/Used_Images/Icons/03_10-temperature-icon.png"
maxWarningColor: Qt.rgba(0.5, 0, 0, 1)
tickmarkLabel: Text {
color: "white"
visible: styleData.value === 0 || styleData.value === 1
font.pixelSize: tempGaugeStyle.toPixels(0.225)
text: styleData.value === 0 ? "C" : (styleData.value === 1 ? "H" : "")
}
}
}
}
CircularGauge {
id: speedometer
value: valueSource.kph
anchors.verticalCenter: parent.verticalCenter
maximumValue: 280
// We set the width to the height, because the height will always be
// the more limited factor. Also, all circular controls letterbox
// their contents to ensure that they remain circular. However, we
// don't want to extra space on the left and right of our gauges,
// because they're laid out horizontally, and that would create
// large horizontal gaps between gauges on wide screens.
width: height
height: container.height * 0.5
style: DashboardGaugeStyle {
}
}
CircularGauge {
id: tachometer
width: height
height: container.height * 0.25 - gaugeRow.spacing
value: valueSource.rpm
maximumValue: 8
anchors.verticalCenter: parent.verticalCenter
style: TachometerStyle {
}
}
TurnIndicator {
id: rightIndicator
anchors.verticalCenter: parent.verticalCenter
width: height
height: container.height * 0.1 - gaugeRow.spacing
direction: Qt.RightArrow
on: valueSource.turnSignal === Qt.RightArrow
}
}
}
}
DashboardGaugeStyle.qml
:
import QtQuick 2.2
import QtQuick.Controls.Styles 1.4
CircularGaugeStyle {
tickmarkInset: toPixels(0.04)
minorTickmarkInset: tickmarkInset
labelStepSize: 20
labelInset: toPixels(0.23)
property real xCenter: outerRadius
property real yCenter: outerRadius
property real needleLength: outerRadius - tickmarkInset * 1.25
property real needleTipWidth: toPixels(0.02)
property real needleBaseWidth: toPixels(0.06)
property bool halfGauge: false
function toPixels(percentage) {
return percentage * outerRadius;
}
function degToRad(degrees) {
return degrees * (Math.PI / 180);
}
function radToDeg(radians) {
return radians * (180 / Math.PI);
}
function paintBackground(ctx) {
if (halfGauge) {
ctx.beginPath();
ctx.rect(0, 0, ctx.canvas.width, ctx.canvas.height / 2);
ctx.clip();
}
ctx.beginPath();
ctx.fillStyle = "black";
ctx.ellipse(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fill();
ctx.beginPath();
ctx.lineWidth = tickmarkInset;
ctx.strokeStyle = "black";
ctx.arc(xCenter, yCenter, outerRadius - ctx.lineWidth / 2, outerRadius - ctx.lineWidth / 2, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
ctx.lineWidth = tickmarkInset / 2;
ctx.strokeStyle = "#222";
ctx.arc(xCenter, yCenter, outerRadius - ctx.lineWidth / 2, outerRadius - ctx.lineWidth / 2, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
var gradient = ctx.createRadialGradient(xCenter, yCenter, 0, xCenter, yCenter, outerRadius * 1.5);
gradient.addColorStop(0, Qt.rgba(1, 1, 1, 0));
gradient.addColorStop(0.7, Qt.rgba(1, 1, 1, 0.13));
gradient.addColorStop(1, Qt.rgba(1, 1, 1, 1));
ctx.fillStyle = gradient;
ctx.arc(xCenter, yCenter, outerRadius - tickmarkInset, outerRadius - tickmarkInset, 0, Math.PI * 2);
ctx.fill();
}
background: Canvas {
onPaint: {
var ctx = getContext("2d");
ctx.reset();
paintBackground(ctx);
}
Text {
id: speedText
font.pixelSize: toPixels(0.3)
text: kphInt
color: "white"
horizontalAlignment: Text.AlignRight
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.verticalCenter
anchors.topMargin: toPixels(0.1)
readonly property int kphInt: control.value
}
Text {
text: "km/h"
color: "white"
font.pixelSize: toPixels(0.09)
anchors.top: speedText.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
}
needle: Canvas {
implicitWidth: needleBaseWidth
implicitHeight: needleLength
property real xCenter: width / 2
property real yCenter: height / 2
onPaint: {
var ctx = getContext("2d");
ctx.reset();
ctx.beginPath();
ctx.moveTo(xCenter, height);
ctx.lineTo(xCenter - needleBaseWidth / 2, height - needleBaseWidth / 2);
ctx.lineTo(xCenter - needleTipWidth / 2, 0);
ctx.lineTo(xCenter, yCenter - needleLength);
ctx.lineTo(xCenter, 0);
ctx.closePath();
ctx.fillStyle = Qt.rgba(0.66, 0, 0, 0.66);
ctx.fill();
ctx.beginPath();
ctx.moveTo(xCenter, height)
ctx.lineTo(width, height - needleBaseWidth / 2);
ctx.lineTo(xCenter + needleTipWidth / 2, 0);
ctx.lineTo(xCenter, 0);
ctx.closePath();
ctx.fillStyle = Qt.lighter(Qt.rgba(0.66, 0, 0, 0.66));
ctx.fill();
}
}
foreground: null
}
IconGaugeStyle.qml
:
import QtQuick 2.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
DashboardGaugeStyle {
id: fuelGaugeStyle
minimumValueAngle: -60
maximumValueAngle: 60
tickmarkStepSize: 1
labelStepSize: 1
labelInset: toPixels(-0.25)
minorTickmarkCount: 3
needleLength: toPixels(0.85)
needleBaseWidth: toPixels(0.08)
needleTipWidth: toPixels(0.03)
halfGauge: true
property string icon: ""
property color minWarningColor: "transparent"
property color maxWarningColor: "transparent"
readonly property real minWarningStartAngle: minimumValueAngle - 90
readonly property real maxWarningStartAngle: maximumValueAngle - 90
tickmark: Rectangle {
implicitWidth: toPixels(0.06)
antialiasing: true
implicitHeight: toPixels(0.2)
color: "#c8c8c8"
}
minorTickmark: Rectangle {
implicitWidth: toPixels(0.03)
antialiasing: true
implicitHeight: toPixels(0.15)
color: "#c8c8c8"
}
background: Item {
Canvas {
anchors.fill: parent
onPaint: {
var ctx = getContext("2d");
ctx.reset();
paintBackground(ctx);
if (minWarningColor != "transparent") {
ctx.beginPath();
ctx.lineWidth = fuelGaugeStyle.toPixels(0.08);
ctx.strokeStyle = minWarningColor;
ctx.arc(outerRadius, outerRadius,
// Start the line in from the decorations, and account for the width of the line itself.
outerRadius - tickmarkInset - ctx.lineWidth / 2,
degToRad(minWarningStartAngle),
degToRad(minWarningStartAngle + angleRange / (minorTickmarkCount + 1)), false);
ctx.stroke();
}
if (maxWarningColor != "transparent") {
ctx.beginPath();
ctx.lineWidth = fuelGaugeStyle.toPixels(0.08);
ctx.strokeStyle = maxWarningColor;
ctx.arc(outerRadius, outerRadius,
// Start the line in from the decorations, and account for the width of the line itself.
outerRadius - tickmarkInset - ctx.lineWidth / 2,
degToRad(maxWarningStartAngle - angleRange / (minorTickmarkCount + 1)),
degToRad(maxWarningStartAngle), false);
ctx.stroke();
}
}
}
Image {
source: icon
anchors.bottom: parent.verticalCenter
anchors.bottomMargin: toPixels(0.3)
anchors.horizontalCenter: parent.horizontalCenter
width: toPixels(0.3)
height: width
fillMode: Image.PreserveAspectFit
}
}
}
TachometerStyle.qml
:
import QtQuick 2.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
DashboardGaugeStyle {
id: tachometerStyle
tickmarkStepSize: 1
labelStepSize: 1
needleLength: toPixels(0.85)
needleBaseWidth: toPixels(0.08)
needleTipWidth: toPixels(0.03)
tickmark: Rectangle {
implicitWidth: toPixels(0.03)
antialiasing: true
implicitHeight: toPixels(0.08)
color: styleData.index === 7 || styleData.index === 8 ? Qt.rgba(0.5, 0, 0, 1) : "#c8c8c8"
}
minorTickmark: null
tickmarkLabel: Text {
font.pixelSize: Math.max(6, toPixels(0.12))
text: styleData.value
color: styleData.index === 7 || styleData.index === 8 ? Qt.rgba(0.5, 0, 0, 1) : "#c8c8c8"
antialiasing: true
}
background: Canvas {
onPaint: {
var ctx = getContext("2d");
ctx.reset();
paintBackground(ctx);
ctx.beginPath();
ctx.lineWidth = tachometerStyle.toPixels(0.08);
ctx.strokeStyle = Qt.rgba(0.5, 0, 0, 1);
var warningCircumference = maximumValueAngle - minimumValueAngle * 0.1;
var startAngle = maximumValueAngle - 90;
ctx.arc(outerRadius, outerRadius,
// Start the line in from the decorations, and account for the width of the line itself.
outerRadius - tickmarkInset - ctx.lineWidth / 2,
degToRad(startAngle - angleRange / 8 + angleRange * 0.015),
degToRad(startAngle - angleRange * 0.015), false);
ctx.stroke();
}
Text {
id: rpmText
font.pixelSize: tachometerStyle.toPixels(0.3)
text: rpmInt
color: "white"
horizontalAlignment: Text.AlignRight
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.verticalCenter
anchors.topMargin: 20
readonly property int rpmInt: valueSource.rpm
}
Text {
text: "x1000"
color: "white"
font.pixelSize: tachometerStyle.toPixels(0.1)
anchors.top: parent.top
anchors.topMargin: parent.height / 4
anchors.horizontalCenter: parent.horizontalCenter
}
Text {
text: "RPM"
color: