Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在QML中访问父函数_Javascript_Qml_Blackberry 10 - Fatal编程技术网

Javascript 在QML中访问父函数

Javascript 在QML中访问父函数,javascript,qml,blackberry-10,Javascript,Qml,Blackberry 10,我目前正在创建一个Twitter客户端,作为学习为BlackBerry10开发的一种方式。我目前正在尝试为自定义ListView项目创建一个上下文菜单,在选择时,它将在主布局中显示一个对话框。但是,我无法使所选列表项从父页面调用任何函数 以下是我的自定义列表项中的QML: import bb.cascades 1.0 Container { property alias avatar: tracker.imageSource property alias username:

我目前正在创建一个Twitter客户端,作为学习为BlackBerry10开发的一种方式。我目前正在尝试为自定义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>...

    }
}
导入bb.cascades 1.0
容器{
属性别名avatar:tracker.imageSource
属性别名用户名:txtweetUser.text
属性别名tweetText:txTweetContent.text
属性别名tweetTime:txTweetTime.text
信号发送回复(字符串用户名)
函数cout(文本){
console.debug(“[debug]”+文本);
}
id:itemTweet
首选宽度:768;
首选身高:200
//行动
上下文操作:[
动作集{
标题:“动作集”
字幕:“这是一个动作集。”
行动:[
行动项目{
标题:“答复”
图像源:“asset:///reply.png"
反对:{
itemTweet.sendReply(txTweetUser.text);
}
},
行动项目{
标题:“转发”
图像源:“asset:///retweet.png"
},
行动项目{
标题:“最爱”
图像源:“asset:///fav.png"
}
]
}//操作集结束
]//contextActions列表结束
...
}
}
对于主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> ...

}
导入bb.cascades 1.0
选项卡窗格{
id:选项卡窗格
showTabsOnActionBar:真
标签{
id:tabTimeline
标题:“时间线”
图像源:“asset:///twitter-white.png"
第{
id:pageTimeline
信号开放应答
信号显示新网
函数cout(文本){
console.debug(“[debug]”+文本);
}
函数showTweetWindow(用户名){
pageTimeline.dialogNewTweet.text=用户名;
pageTimeline.dialogNewTweet.visible=true;
}
//标题栏
标题栏:标题栏{
可见性:覆盖
标题:“推特”
接受:行动项目{
id:Btnewtweet
标题:“+”
ActionBar.placement:ActionBarPlacement.OnBar
反对:{
pageTimeline.cout(“所选行动”);
dialogNewTweet.visible=true;
}
}
}
//主要内容
内容:容器{
布局:绝对布局{}
容器{
//tweets的列表视图
列表视图{
id:LSTweets
objectName:“lstweets”
//要显示行的组件
listItemComponents:[
ListItemComponent{
id:listItem
类型:“listItem”
TweetItem{
tweetText:ListItemData.content
tweetTime:ListItemData.time
化身:ListItemData.avatar
用户名:ListItemData.username
答复:{
cout(“在父项中选择回复“+用户名”);
pageTimeline.showTweetWindow(用户名);
}
}
}
]
OnSelection已更改:{
}
函数itemType(数据,indexath){
if(indexath.length==1){
返回“标题”;
}否则{
返回“列表项”;
}
} 
}
}
对话网{
id:dialogNewTweet
可见:假
在ShowNewWeet上:{
dialogNewTweet.visible=true;
}
}
}
//端部容器
}
}
...  ...
}
因此,当主QML文件接收到SendReply信号时,支持调用
showTweetWindow(username)
,然后使
对话框Newtweet
可见,但我得到的却是错误
ReferenceError:找不到变量:pageTimeline
。这肯定是一个范围问题,但我不知道我做错了什么,或者我是否需要对其进行重组。

回答如下:

我需要实现的是在一个数据列表上操作,例如在QML ListItem使用CeExcTebug删除一个项目,然后调用一个对象在C++文档中的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());
还有另一种方法:

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

Page {
    id: topPage    
    onCreationCompleted: { Qt.topLabel = topLabel; }
然后,在按钮定义中,可以从列表中引用Qt.topLabel

    listItemComponents: [
            ListItemComponent {
                type: "item"
                Button {
                    text: "Change To Three"
                    onClicked: {
                        Qt.topLabel.text = "Three";
                    }                        
                }
            }
        ]
    listItemComponents: [
            ListItemComponent {
                type: "item"
                Button {
                    text: "Change To Three"
                    onClicked: {
                        Qt.topLabel.text = "Three";
                    }                        
                }
            }
        ]