![](/img/trans.png)
[英]C++ class exposed to QML error in fashion TypeError: Property '...' of object is not a function
[英]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.