我正在熟悉 QT5 中的 QML。所以我试图从以下视频中编写代码:video
在此视频中使用了代码:send.connect(target.receive());
。但这部分不起作用。我收到错误:
qrc:/Receiver.qml:8: Error: Cannot assign [undefined] to QString
此方法是否已弃用或者我做错了什么?
main.qml
Window {
visible: true
width: 640
height: 480
title: qsTr("Jurassic World")
Background {
id: background
anchors.fill: parent
target: sender
Sender {
id: sender
y: 145
displayText: "Sender"
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: 50
target: receiver
}
Receiver {
id: receiver
x: 376
y: 158
displayText: "Receiver"
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right
anchors.rightMargin: 50
width: sender.width
}
}
}
接收器.qml
Cirkel {
id: receiveButton
function receive(value)
{
displayText = value
clicknotify.running = true
}
SequentialAnimation on buttonColor
{
id: clicknotify
running: false
ColorAnimation {
from: "#afc4cb"
to: "red"
duration: 200
}
ColorAnimation {
from: "red"
to: "#afc4cb"
duration: 200
}
}
}
发件人.qml
Cirkel {
id: sendButton
property int counter: 0
property Receiver target: null
signal send(string value)
onTargetChanged: {
send.connect(target.receive());
}
MouseArea {
anchors.fill: parent
onClicked: {
counter++
parent.send(counter)
displayText = counter
}
onPressed: parent.buttonColor = "green"
onReleased: parent.buttonColor = "#afc4cb"
}
}
问题: 如何将信号从一个 qml 链接到另一个 qml 函数?
最佳答案
你写:
send.connect(target.receive());
在其中调用 target.receive()
并尝试将其返回值连接到 send
。
你想要的是:
send.connect(target.receive);
将 send
连接到函数 receive
本身的位置。
建议:
Also try the declarative way, using a
Connection
-object.
关于qt - QML 将信号连接到函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53482999/