How to access external components from listitemcomponents in listview bb10 qml?

I cannot access the Datasource ID from inside the ListItemComponent. Can anyone help me in this regard?

ListItemComponent {
    type: "item"
    Container {
        id: listviewcontainer
        Container {
            preferredWidth: 768
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            CustomImageView {
                leftPadding: 10
                rightPadding: 10
                url: ListItemData.from_image
                horizontalAlignment: HorizontalAlignment.Left
                verticalAlignment: VerticalAlignment.Center
            }
            Container {
                preferredWidth: 538
                layout: StackLayout {
                    orientation: LayoutOrientation.TopToBottom
                }
                Container {
                    layout: StackLayout {
                        orientation: LayoutOrientation.LeftToRight
                    }
                    Label {
                        text: ListItemData.from
                        textStyle {
                            base: SystemDefaults.TextStyles.TitleText
                            color: Color.create("#2db6ff")
                        }
                    }
                    ImageView {
                        imageSource: "asset:///Home/img.png"
                        verticalAlignment: VerticalAlignment.Center
                    }
                }//Container
                Label {
                    text: ListItemData.message
                    multiline: true
                    textStyle {
                        base: SystemDefaults.TextStyles.SubtitleText
                    }
                    content {
                        flags: TextContentFlag.Emoticons
                    }
                }
                Label {
                    id: time
                    text: ListItemData.time
                    textStyle {
                        base: SystemDefaults.TextStyles.SmallText
                        color: Color.create("#666666")
                    }
                }
            }//Container
            ImageButton {
                id: delete_btn
                defaultImageSource: "asset:///Icon/delete.png"
                pressedImageSource: "asset:///Icon/delete.png"
                verticalAlignment: VerticalAlignment.Center
                horizontalAlignment: HorizontalAlignment.Right
                onClicked: {
                    deleteMessage(ListItemData.tid, ListItemData.uid);
                }
                function deleteMessage(tid, uid) {
                    var request = new XMLHttpRequest()
                    request.onreadystatechange = function() {
                        if (request.readyState == 4) {
                            var mResponse = request.responseText
                            mResponse = JSON.parse(mResponse)
                            var mResponseStatus = mResponse.response[0].receive.status;
                            var mMsg = mResponse.response[0].receive.message;
                            if (mResponseStatus == 1) {
                                msg_DataSource.source = "newurl.com" //   This line not works here..
                                msg_DataSource.load();              //   This line not works here..
                            } else if (mResponseStatus == 0) {
                            }
                        }
                    }// end function
                    request.open("GET", "myurl.com", true);
                    request.send();
                }// deleteMessage
            }//ImageButton
        }//Container
    }//Container
}//ListItemComponent

      

The following two lines fail here

msg_DataSource.source = "newurl.com" 
msg_DataSource.load();  

      

I tried as below but that doesn't work either

listviewcontainer.ListItem.view.dataModel.message_DataSource.source = "myurl.com";
listviewcontainer.ListItem.view.dataModel.message_DataSource.load();

      

or

listviewcontainer.ListItem.view.dataModel.source = "myurl.com"; 
listviewcontainer.ListItem.view.dataModel.load();

      

+3


source to share


2 answers


Another easy way to store an object for a global variable using the following code which works great for me.

         onCreationCompleted: {
         Qt.tabbedPane = tabbedPane;
         Qt.homeTab = homeTab;
        }   

      



Here I have saved the tabbedPane in a global Qt.tabbedPane when the Completed page is created.Now I can access it from the ListItemComponent using Qt.tabbedPane.

Hope it helps.

+1


source


The easiest way to make a data model available is to declare it property alias

in your data model wherever it is defined, for example in your ListView

QML file. This would make your data model available to the top level component in QML from this property alias

. In fact, it gives you a global reference to your data model from anywhere in QML.

For example, if your data model is named msg_DataSource

, then in the QML file where it is defined, you can create a property alias like this:

property alias myDataModel: msg_DataSource

      



Then in your function, ListItemComponent deleteMessage

you can use myDataModel

like this:

myDataModel.source = "newurl.com" 
myDataModel.load();

      

Note. I'm sure you could do it more elegantly using signals and slots, but this way should be faster and easier to understand.

0


source







All Articles