从 QML 中的函数调用? [英] Invocation from function in QML?

查看:41
本文介绍了从 QML 中的函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用页面中的 InvokeActionItem 轻松共享项目,但我需要能够在列表视图项目中调用它.我已经成功触发了一个调用,但我不知道在触发它时如何添加数据.我不断收到

I can share an item easily using an InvokeActionItem in a Page but I need to be able to call it in a listview item. I've managed to trigger an invoke, but I cannot figure out how to add data when triggering it. I keep getting an error message of

InvocationPrivate::setQuery: 你不能改变 InvokeQuery 对象

InvocationPrivate::setQuery: you are not allowed to change InvokeQuery object

注意:我尝试在纯 QML 中执行此操作,如有必要,我将通过 c++ 执行此操作,但 QML 会更好.

Note: I am trying to do this in purely QML, I will do it via c++ if necessary but QML would be preferable.

在 Page 对象中工作的代码:

Code that works inside a Page object:

actions: [
    InvokeActionItem {
        ActionBar.placement: ActionBarPlacement.OnBar
        title: "Share"   
        query {
            mimeType: "text/plain"
            invokeActionId: "bb.action.SHARE"
        }

        onTriggered: {
            //myTextProperty is a string variable property for the page.
            data = myTextProperty;                
        }
    }
]

我尝试在其他项目中使用的代码如下,但不起作用:

The code I've tried to use in the other item is as follows, but does NOT work:

Container {
gestureHandlers: [
    TapHandler {
    LongPressHandler {
        onLongPressed: {
            console.log("Longpress");                

            invokeQuery.setData("test");

            invokeShare.trigger("bb.action.SHARE");
        }            
    }        

]

attachedObjects: [
    Invocation {
       id: invokeShare
       query: InvokeQuery {
           id:invokeQuery
           mimeType: "text/plain"         
       }   
    }
]
}

有没有办法完全使用 QML 来更改调用的数据,还是我只需要通过 c++ 运行它?

Is there a way to change the data for an invoke purely with QML or do I need to just run it through c++ instead?

推荐答案

在浏览了相当多的论坛并测试了各种方法后,我终于找到了一个可行的方法.

After a fair amount of browsing forums and testing various methods, I have finally found one that works.

在您的附加对象中添加以下内容:

Add the following in your attachedObjects:

attachedObjects: [      
    Invocation {
       id: invokeShare
       query: InvokeQuery {
           id:invokeQuery
           mimeType: "text/plain"                        
       }
       onArmed: {
           if (invokeQuery.data != "") {
               trigger("bb.action.SHARE");
           }
       }             
    }
]

然后在需要调用调用的地方执行以下操作:

Then wherever you need to call the invocation do the following:

invokeQuery.mimeType = "text/plain"
invokeQuery.data = "mytext";
invokeQuery.updateQuery();

请注意,如果您不检查 onArmed 中的数据,它将自动调用创建时的调用 - 对于列表视图,这可能会导致 20 多个屏幕要求您在 bbm 上共享...;)

Note that if you do not do a check in the onArmed for data it will automatically call the invocation on creation - in the case of a listview this can result in 20+ screens asking you to share on bbm... ;)

这篇关于从 QML 中的函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆