1
Qt Quick 5.15.0 Connections 新语法
source link: https://www.mycode.net.cn/language/2941.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Qt Quick 5.15.0 Connections 新语法
过去的 Connection 连接某个对象的信号时是这样写的:
Connections {
target: authManager
onError: {
busyContainer.visible = false
buttonJoin.enabled = Qt.binding(function () {
return textMeetingId.length >= 11 && textNickname.length > 0
})
message.error(result.msg)
}
}
新的语法中可以这样写:
Connections {
target: authManager
function onError(resCode, result) {
busyContainer.visible = false
buttonJoin.enabled = Qt.binding(function () {
return textMeetingId.length >= 11 && textNickname.length > 0
})
message.error(result.msg)
}
}
对于日常开发中,新的写法有助于我们定义槽函数的形参个数和实际作用。代码看起来更容易读懂。另外 Qt 官方文档中也介绍了一些其他好处,见:Qt官方文档
如果你使用 Qt 15.5.0 版本打开了一个旧的工程并使用的是旧的写法,那么会收到一个警告:
qrc:/qml/AnonJoinPage.qml:54:5: QML Connections: Implicitly defined onFoo properties in Connections are deprecated. Use this syntax instead: function onFoo(<arguments>) { ... }
既然是个警告,则不影响代码继续执行。但这样的警告不得不让你重写这部分代码来适配新的语法。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK