當前位置:編程學習大全網 - 網站源碼 - 2.如何在QML中調用C++的方法並接收C++的信號

2.如何在QML中調用C++的方法並接收C++的信號

在QML中調用c++方法並接收信號

繼續上壹篇的內容使用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();

...

};

  • 上一篇:頭條思什麽軟件?
  • 下一篇:補碼10001源代碼。
  • copyright 2024編程學習大全網