繼續上壹篇的內容使用C++創建新的QML類型,接下來我們在PieChart 這個類中添加壹個函數"clearChart()" 和壹個信號"chartCleared",這樣在app.qml中就可以像下面壹樣調用這個函數,並接收這個信號了:
import Charts 1.0
import QtQuick 1.0
Item {
width: 300; height: 200
PieChart {
id: aPieChart
anchors.centerIn: parent
width: 100; height: 100
color: "red"
onChartCleared: console.log("The chart has been cleared")
}
MouseArea {
anchors.fill: parent
onClicked: aPieChart.clearChart()
}
Text {
anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 }
text: "Click anywhere to clear the chart"
}
}
File:SimpleChart2.png?
為c++類添加被調用的方法和信號
下面我們就來看壹下在C++的類中我們具體應該怎麽做:
class PieChart : public QDeclarativeItem
{
...
public:
...
Q_INVOKABLE void clearChart();
signals:
void chartCleared();
...
};