qt - QML TypeError:对象yy的属性'xx'不是函数
问题描述
我有这个 main.qml:
import QtQuick 2.13
import QtQuick.Window 2.13
import QtQuick.Controls 2.5
ApplicationWindow {
...
Item {
// This Item is to provide needed properties for functionality in the original app that was elided out in this example.
// It was left in in case it's relevant to the problem.
...
Column {
...
Text {
text: qsTr("Masked text")
SlidingMask {
id: testMask
anchors.fill: parent
...
}
}
Row {
Button {
id: btnRevealText
text: qsTr("Reveal")
...
}
Button {
id: btnHideText
text: qsTr("Hide")
...
}
}
}
}
Connections {
target: btnRevealText
onPressed: testMask.reveal()
}
Connections {
target: btnHideText
onPressed: testMask.hide()
}
}
而这个在 qml.qrc 中注册的 SlidingMask.qml:
import QtQuick 2.0
Rectangle {
...
function hide() {
...
}
function reveal() {
...
}
}
当我运行应用程序并尝试按下按钮时,出现以下错误:
TypeError: Property 'hide' of object SlidingMask_QMLTYPE_7(0x19991132c50) is not a function
TypeError: Property 'reveal' of object SlidingMask_QMLTYPE_7(0x19991132c50) is not a function
但是,如果我尝试更改 Connections 以更改 SlidingMask 的属性而不是调用函数,它可以正常工作。
我之前也测试过这个组件并且没有遇到任何问题,尽管我没有在那个测试中使用 Connections。
我在这里和谷歌上搜索了答案,但我发现的任何东西似乎都与我的情况无关。我将如何解决这个问题?
解决方案
这是一个正常工作的简单示例:
//main.qml
ApplicationWindow {
id: window
width: 640
height: 480
visible: true
Column{
ItemWithFunction{
id: sc
width: 100
height: 100
}
Button{
id: btn1
text: 'Test Connection'
}
}
Connections{
target: btn1
onPressed: sc.testFunction();
}
}
//ItemWithFunction.qml
Rectangle{
color: 'red'
function testFunction(){
console.log("SOMETHING HAPPENED")
}
}
似乎您没有将函数放在SlidingMask
根目录中,而是放在其子组件之一中。
推荐阅读
- angular - 如何在输入上创建固定只读前缀?
- elastic-stack - 是否使用 docker 运行 Beats?
- c++ - CLion、SDL2、CMake:没有可用的视频设备
- ios - 当我们在swift中使用MKMapView点击mapView时如何移动到第二个Viewcontroller?
- swift - 无法在集合视图上获取 indexPath 和选择
- java - 如何在 JSP 页面中显示列表结果?
- html - 如何在 Apple tv 应用程序中打开 HTML 文件?
- swift - 滚动表格视图时删除的文本字段值?
- laravel - Laravel 无法附加多对多表
- html - 鼠标在按钮中间时菜单不可点击