Qt 函数类型的对象属性

Qt 函数类型的对象属性,qt,qml,Qt,Qml,我有一个interstitularadQML对象(QtQuick 2,QT 5.13),它带有onClosed事件,由interstitularad关闭触发。在开始新游戏之前,我尝试使用以下QML代码显示中间广告: InterstitialAd { id: iAd property variant handlerFunc onClosed: { if (handlerFunc) { handlerFunc

我有一个
interstitularad
QML对象(QtQuick 2,QT 5.13),它带有
onClosed
事件,由interstitularad关闭触发。在开始新游戏之前,我尝试使用以下QML代码显示中间广告:

InterstitialAd {
    id: iAd

    property variant handlerFunc

    onClosed: {
        if (handlerFunc) {
            handlerFunc
            handlerFunc = null
        }
    }
}

function resetGameWithAd()
{
    iAd.handlerFunc = Qt.binding(function() {
        console.log("AdTest: calling resetGame()")
        scene.resetGame()
    })
    console.log("AdTest: calling iAd.show()")
    iAd.show()
}
在这里,我尝试将
handlerFunc
分配给一个函数,该函数在触发
onClosed
事件时重新启动游戏,但得到了我意想不到的效果。我的应用程序的控制台输出为:

qml: AdTest: calling resetGame()
qml: AdTest: calling iAd.show()
显然,将
handlerFunc
赋值给
Qt.binding…
实际上调用了该函数(因为
resetGame
是先打印的),但我希望它只执行赋值。演示了类似的技术

什么是错误的,什么是正确的实施方法?

我也尝试过这样的代码:

    function resetGameHandler(params)
    {
        iAd.closed.connect(function() {
            scene.resetGame(params)
            iAd.closed.disconnect(/*...???...*/) 
        })
        iAd.show();
    }
但没有成功,因为(据我看来,这意味着我需要一个带有名称的正则函数)

我已经好几个月没有做任何QML了,所以我可能错了。但是如果我的记忆力好的话,这可能会对你有所帮助

要接近您的方法,请执行以下操作:

  • 变体
    已弃用。建议改用
    var
  • 您不需要
    Qt.binding()
    。可以直接将函数指定给该属性
  • 调用属性中的函数

  • 间隙翼{ id:iAd 属性var handlerFunc 我已经好几个月没有做任何QML了,所以我可能错了。但是如果我的记忆力好的话,这可能会对你有所帮助

    要接近您的方法,请执行以下操作:

  • variant
    已被弃用。建议改用
    var
  • 您不需要
    Qt.binding()
    。您可以直接为该属性分配函数
  • 调用属性中的函数

  • 间隙翼{ id:iAd
    “属性var Vall?rFrcc,为什么不登记回调,而不是直接绑定处理程序?@ C++ DRM,当用户1启动新游戏2时显示广告(改变游戏参数3),当游戏超过4)……所以我理想的是,我需要一些类似于<代码> STD::函数< /COD> C++,当广告被关闭时,将调用它。不登记回调,而不是直接绑定一个处理程序?@用户在1用户开始新游戏2时显示广告(改变游戏参数3),当游戏超过4)时……所以我理想地需要在C++关闭时调用C++中的“代码> STD:函数< /代码>。
    InterstitialAd {
        id: iAd
    
        property var handlerFunc <-- Use var instead of variant
    
        onClosed: {
            if (handlerFunc && typeof handlerFunc === "function") {
                handlerFunc() <-- Call it!
                handlerFunc = null
            }
        }
    }
    
    iAd.handlerFunc = function() { // doSomething cool here }