Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
grenadedylex
Posts: 73
Registered: ‎04-08-2014
My Device: Z10
My Carrier: XL

How to refresh a qml using button?

hi 

just like subject above

how can i refresh/ reload entire page (qml) using a single button,? thanks

 

Please use plain text.
Developer
bmorr
Posts: 318
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: How to refresh a qml using button?

Unfortunately this cannot be done strictly in QML, you'd have to setup a convoluted function that reloads the qml through c++.

 

Maybe if you explained why you needed to refresh, we could help.  I suspect it's because saved data isn't updating on the page?

Please use plain text.
Developer
grenadedylex
Posts: 73
Registered: ‎04-08-2014
My Device: Z10
My Carrier: XL

Re: How to refresh a qml using button?

thanks u
i need to refresh it because in my app theres a add comment
if i want to add comment that means all comment must be refresh
i have function to call all the comment, but when i try second call which is refresh, the JSON cant be parse
why json cant be parse at the second call? thanks
Please use plain text.
Developer
anditsung
Posts: 243
Registered: ‎09-30-2010
My Device: Z10, Onyx2
My Carrier: IND XL

Re: How to refresh a qml using button?

how the comment is showing?
if you were using a listview you could use groupdatamodel for the comment. it will automaticaly add to the list
Please use plain text.
Developer
grenadedylex
Posts: 73
Registered: ‎04-08-2014
My Device: Z10
My Carrier: XL

Re: How to refresh a qml using button?

thanks anditsung
if i use groupdatamodel for all mycomment, it will show nothing. so i choose ArrayDataModel and all of mycomment show
Please use plain text.
Developer
MagnumOpus
Posts: 138
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: How to refresh a qml using button?

We could help you better if you show us how you are trying to refresh the dataModel.

Regards.

If my answer has been useful to you, please, click the button
Please use plain text.
Developer
bmorr
Posts: 318
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: How to refresh a qml using button?

what type of data are you trying to populate your list with?  an RSS feed, XML, JSON, SQL?

Please use plain text.
Developer
greenback
Posts: 527
Registered: ‎10-17-2010
My Device: BlackBerry Z10, DAC

Re: How to refresh a qml using button?

Hello, if you are using an ArrayDataModel you can use the append method to add/visually update your list to display your new comment, like so:

 

ListView {
    dataModel: ArrayDataModel {
        id: theDataModel
    }
    onCreationCompleted: {
        //prepop the data model for testing purposes
        for ( var a = 0; a < 20; a++ ) {
            theDataModel.append("Comment" + a);
        }
    }
}

TextField{
    id: comment
    hintText: "Write your comment...";
}
Button{
   text: "Post Comment"
   onClicked: {
       //grab comment content and append
       theDataModel.append([comment.text]);
   }
}

 

You can also use the clear() method on your datamodel to clear the list and then run an HTTP query to pull the data, and then populate your list using a for loop.

 

Hope that helps!

Please use plain text.
Developer
grenadedylex
Posts: 73
Registered: ‎04-08-2014
My Device: Z10
My Carrier: XL

Re: How to refresh a qml using button?

[ Edited ]

thank you greenback, bmor and magnumOpus

oke so this is how i show my comment

this type data is JSON (webservice)

 

my c++ (nothing wrong with this)

void ApplicationUI::comment(const QString& id)
{
    //manager = new QNetworkAccessManager(this);

    QUrl url("http://myurl");
    url.addQueryItem("method", "comment");
    url.addQueryItem("id", id);
    url.addQueryItem("regionid", "1");

    QNetworkRequest req(url);

    QNetworkReply *reply = manager->get(req);
    connect(reply, SIGNAL(finished()), this, SLOT(commentFinished()));
}


void ApplicationUI::commentFinished()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());

    if (!reply->error()){

       QByteArray response = reply->readAll();
                        response.replace("\\", "");
                        if (response.startsWith("\"")){
                            response.remove(0, 1);
                        }
                        if (response.endsWith("\"")){
                            response.remove(response.length()-1, 1);
                        }
        emit commentDone (response);
    }else{
        const int httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        qDebug() << "ErrorCode" << httpCode << endl << \
                "ErrorString" << reply->errorString();
        emit error(httpCode, reply->errorString());
    }
    reply->deleteLater();

 

my function to call comment

function comment(response){
        model1.clear()
        console.log("Response: "+response)
        var json1 = JSON.parse(response)
        if(json1){
            model1.append(json1.review_result)
        }
    
    }

 this is my onCreationCompleted which connect to my function

onCreationCompleted: {
        App.commentDone.connect(comment)
    }

 and this is my "void applicationUI from my C++"

 onBusinessChanged: {
       
                App.comment(business.id);
               }

 this is my model

dataModel: ArrayDataModel {
                     id: model1
                                
                  }     
                  listItemComponents: [  
                  ListItemComponent {
                       type: ""
                            Container{
                                layout: StackLayout {
                                       orientation: LayoutOrientation.TopToBottom
                                     }
                                     Container{
                                       layout: StackLayout {
                                          orientation: LayoutOrientation.LeftToRight
                                          }
                                        Container{
                                            layout: StackLayout {
                                                orientation: LayoutOrientation.TopToBottom
                                            }
                                           Label{
                                                text: ListItemData.personName 
                                                 textStyle {
                                             fontWeight: FontWeight.Bold
                                             base: SystemDefaults.TextStyles.BodyText
                                                    }
                                                }
                                              Label{
                                           translationY : -30
                                           text : "Date post : " + ListItemData.reviewDate
                                          textStyle{
                                           base : SystemDefaults.TextStyles.SubtitleText
                                                   }
                                               }
                                           }......//so-on

 i just need to refresh the model1, so when i add comment, it will completely "load", just like apps start (not add it to model1)

any help? thanks :smileyhappy:

hope you guys understand

Please use plain text.
Developer
MagnumOpus
Posts: 138
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: How to refresh a qml using button?

[ Edited ]

If you want to reload the json you can try this and call the dataSource's load method from and actionItem or from wherever you want.

 

    attachedObjects: [
        DataSource {
            id: datasource
            source: YOUR JSON FILE
            onDataLoaded: {
                YOUR_DATAMODEL.clear();
                YOUR_DATAMODEL.append(data);
            }
        }
    ]
    onCreationCompleted: {
        datasource.load();
    }

 

Regards.

If my answer has been useful to you, please, click the button
Please use plain text.