qt - QML 将信号连接到函数

标签 qt qml

我正在熟悉 QT5 中的 QML。所以我试图从以下视频中编写代码:video

在此视频中使用了代码:send.connect(target.receive());。但这部分不起作用。我收到错误:

qrc:/Receiver.qml:8: Error: Cannot assign [undefined] to QString

此方法是否已弃用或者我做错了什么?

ma​​in.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/

相关文章:

c++ - 在 QTreeView 中异步加载数据

qt - QVectors 的 QHash

c++ - QtContainer 中的 QStackedWidget 中的双重删除

c - 二进制文件只有在创建指向它的符号链接(symbolic link)后运行时才能运行

javascript - QML - 事件发生时动态创建计时器

Qt Quick QML MouseArea 按下时自动重复

c++ - Qt 捕捉按下的键

c++ - 即使直接从官方示例复制,也找不到未知类型名称 QML_ELEMENT 和 QML 模块

qt - 检测鼠标光标何时位于 Qt5 和 QML 中的不规则形状图片上

qt - 无法通过 SplitView 中的 id 访问 QML 项目