4

我目前正在创建一个 Twitter 客户端,作为学习为 Blackberry 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。这绝对是一个范围问题,但我无法弄清楚我做错了什么,或者我是否需要重组它。

4

1 回答 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>();

//Here set the context property, accountsManager was already created
qml->setContextProperty("accountsManager", accountsManager);

//Set the model for the list view
ListView* accountsListView = root->findChild<ListView*>("accountsListView");
accountsListView->setDataModel(accountsManager->getDataModel());

QML 代码:

Page {
    content: Container {     
        ListView {
            id: listView
            objectName: "accountsListView"     

        listItemComponents: [
                ListItemComponent {
                    type: "listItem"
                    StandardListItem{
                        id: accountItem
                        title: ListItemData.username                        
                        contextActions: [
                            ActionSet {
                                deleteAction: DeleteActionItem {
                                    title: "Delete"
                                    onTriggered: {
                                        //it does not work here, "ReferenceError: Can't find variable: accountsManager"
                                        accountsManager.doSometing();
                                    }
                                }
                            }
                        ]
                    }
                } 
            ]

        function itemType(data, indexPath) {
            return "listItem";
          }     
        } // end of ListView
    } // end of Container

    actions: [
        ActionItem {
            title: "Add account"
            ActionBar.placement: ActionBarPlacement.OnBar

            onTriggered: {
                //It works fine here
                accountsManager.doSomething();
            }
        }
    ]
} // end of Page

还有另一种方法:

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

在顶部页面级别添加以下行以使标签可访问。

Page {
    id: topPage    
    onCreationCompleted: { Qt.topLabel = topLabel; }

然后在按钮定义中,您可以从列表中引用 Qt.topLabel。

    listItemComponents: [
            ListItemComponent {
                type: "item"
                Button {
                    text: "Change To Three"
                    onClicked: {
                        Qt.topLabel.text = "Three";
                    }                        
                }
            }
        ]
于 2012-10-02T16:25:28.490 回答