代码拉取完成,页面将自动刷新
这是 一个描述 c++与 QML 通过信号槽的形式链接
import QtQuick 2.4 Rectangle {
id: rectangle1
signal receiveText(string str) // c++ 发送信号, qml 接收
signal sendText(string str) // qml 发送信号, c++ 接收
Rectangle
{
anchors.fill: parent
color: "red"
radius: 10
Rectangle{
id:reg
width: 100
height: 100
anchors.centerIn: parent
radius: 50
color: "#cccccc"
Text {
id: name
anchors.centerIn: parent
font.pixelSize: 18
font.bold: true
text: qsTr("")
}
MouseArea
{
anchors.fill: parent
onClicked:
{
sendText("123456") // 鼠标点击 发送信号
name.text = "Send"
num.start()
}
}
NumberAnimation{
id:num
target: reg
property: "scale"
from:1
to:0.3
duration: 2000
}
NumberAnimation{
id:num1
target: reg
property: "scale"
to:1
duration: 2000
}
}
}
onReceiveText: // 当 c++ 发送信号, qml 接收
{
name.text = "Recive"
console.log("aaaaaaaaaaaaaaaaaaaaaaa")
num1.start()
}
}
QQuickItem *item = ui->quickWidget->rootObject();
connect((QObject*)(item),SIGNAL(sendText(QString)),this,SLOT(receiveFromQml(QString)));
// 这里qml 的槽函数, 是通过qnl 的信号来触发的。 所以只能通过按钮的信号来触发 qml 的信号
connect(this,SIGNAL(sendToQml(QString)),(QObject*)(item),SIGNAL(receiveText(QString)));
// c++ 按钮点击发送信号。
connect(ui->pushButton,&QPushButton::clicked,[this]
{
emit sendToQml("yangsen");
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。