首页 文章

在QML中访问父函数

提问于
浏览
4

我目前正在创建一个Twitter客户端,作为学习黑莓10开发的一种方式 . 我目前正在尝试为自定义ListView项创建一个上下文菜单,在选择时将在主布局中显示一个对话框 . 但是,我无法从选定的列表项中调用父页面中的任何函数 .

这是我的自定义列表项中的QML:

import bb.cascades 1.0

Container {

    property alias avatar: tracker.imageSource
    property alias username: txtTweetUser.text
    property alias tweetText: txtTweetContent.text
    property alias tweetTime: txtTweetTime.text

    signal sendReply(string username)

    function cout(text) {
        console.debug("[DEBUG] " + text);
    }

    id: itemTweet

    preferredWidth: 768;
    preferredHeight: 200

    // Actions
    contextActions: [
        ActionSet {


            title: "Action Set"
            subtitle: "This is an action set."

            actions: [
                ActionItem {                    
                    title: "Reply"
                    imageSource: "asset:///reply.png"
                    onTriggered: {
                        itemTweet.sendReply(txtTweetUser.text);
                    }
                },
                ActionItem {
                    title: "Retweet"
                    imageSource: "asset:///retweet.png"
                },
                ActionItem {
                    title: "Favourite"
                    imageSource: "asset:///fav.png"
                }
            ]
        } // end of ActionSet   
    ] // end of contextActions list

   <Layout for the List Item>...

    }
}

对于主要的QML文件:

import bb.cascades 1.0

TabbedPane {
    id: tabbedPane
    showTabsOnActionBar: true

    Tab {
        id: tabTimeline
        title: "Timeline"
        imageSource: "asset:///twitter-white.png"

        Page {         
            id: pageTimeline

            signal openReply
            signal showNewTweet

            function cout(text) {
            console.debug("[DEBUG] " + text);
        }

            function showTweetWindow(username) {
                pageTimeline.dialogNewTweet.text = username;
                pageTimeline.dialogNewTweet.visible = true;
            }

            // Title bar
            titleBar: TitleBar {
                visibility: Overlay
                title: "Twitter"

                acceptAction: ActionItem {
                    id: btnNewTweet
                    title: "+"
                    ActionBar.placement: ActionBarPlacement.OnBar
                    onTriggered: {
                       pageTimeline.cout("action selected");
                       dialogNewTweet.visible = true;
                    }
                }
            }

            // Main content
            content: Container {

                layout: AbsoluteLayout {}

                Container { 
                    // Listview for the tweets
                    ListView {
                        id: lstTweets
                        objectName: "lstTweets"                 

                // Components to display the rows                   
                        listItemComponents: [
                            ListItemComponent {
                                id: listItem
                                type: "listItem"
                                TweetItem {                                                                                     
                                    tweetText: ListItemData.content
                                    tweetTime: ListItemData.time
                                    avatar: ListItemData.avatar
                                    username: ListItemData.username

                                    onSendReply: {
                                        cout("Reply selected in parent to " + username);
                                        pageTimeline.showTweetWindow(username);
                                    }
                                }
                            }
                        ]

                        onSelectionChanged: {
                        }

                        function itemType(data, indexPath) {
                            if (indexPath.length == 1) {
                                return "header";
                            } else {
                                return "listItem";
                            }
                        } 

                    }
                }

                DialogNewTweet {
                    id: dialogNewTweet
                    visible: false

                    onShowNewTweet: {
                        dialogNewTweet.visible = true;
                    }
                }

            }
            // End container    
        }
    }
    ... <Other tabs> ...

}

因此,当主QML文件收到SendReply信号时,它会调用 showTweetWindow(username) 然后使 dialogNewTweet 可见,但我得到错误 ReferenceError: Can't find variable: pageTimeline . 它's definitely a scope issue, but I can'弄清楚我做错了什么,或者我是否需要对其进行重组 .

1 回答

  • 3

    在这里回答:

    https://community.blackberry.com/message/182467#182467

    我需要实现的是对数据列表进行操作,例如:在QML中使用ListItem中的ContextAction删除项目,然后在QmlDocument的contextProperty中调用对象的C方法 . 这是我如何做到的:C代码:QmlDocument * qml = QmlDocument :: create(“accounts.qml”);
    root = qml-> createRootNode <AbstractPane>();

    //这里设置了context属性,accountsManager已经创建
    qml-> setContextProperty(“accountsManager”,accountsManager);

    //设置列表视图的模型
    ListView * accountsListView = root-> findChild <ListView *>(“accountsListView”);
    accountsListView-> setDataModel(accountsManager-> getDataModel());
    QML代码:Page {
    内容:容器{
    列表显示 {
    id:listView
    objectName:“accountsListView”

    listItemComponents:[
    ListItemComponent {
    类型:“listItem”
    StandardListItem {
    id:accountItem
    title:ListItemData.username
    contextActions:[
    ActionSet {
    deleteAction:DeleteActionItem {
    Headers :“删除”
    onTriggered:{
    //它在这里不起作用,“ReferenceError:找不到变量:accountsManager”
    accountsManager.doSometing();
    }
    }
    }
    ]
    }
    }
    ]

    function itemType(data,indexPath){
    return“listItem”;
    }
    } // ListView的结尾
    } // Container的结尾

    行动:[
    行动项目 {
    Headers :“添加帐户”
    ActionBar.placement:ActionBarPlacement.OnBar

    onTriggered:{
    //这里运作正常
    accountsManager.doSomething();
    }
    }
    ]
    } //页面结尾

    另一种方法:

    https://community.blackberry.com/message/453794#453794

    在顶层页面级别添加以下行以使标签可访问 . 第{{
    id:topPage
    onCreationCompleted:

    然后在按钮定义中,您可以从列表中引用Qt.topLabel . listItemComponents:[
    ListItemComponent {
    类型:“项目”
    按钮{
    文字:“改为三”
    onClicked:

    }
    }
    ]

相关问题