繁体   English   中英

QML TypeError:对象 yy 的属性“xx”不是函数

[英]QML TypeError: Property 'xx' of object yy is not a function

我有这个 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

但是,如果我尝试更改连接以更改 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根中,而是放在其子组件之一中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM