0

I am loading JSON from a web service with a QNetworkRequest which I then put into a JsonDataAccess object. I can parse the JSON and everything is fine.

But I want to format the JSON string to HTML and display it in a WebView which seems to not work from the thread that the finished(QNetworkReply*) signal uses, since it's supposed to be non-UI-blocking.

However, I can add markers to a MapView with the very same method. But updating the WebView does not work. How do I get to the main thread to update the WebView?

Here's the method (slot) I use at the moment:

void ApplicationUI::onLoadJsonFinished(QNetworkReply* reply) {

    JsonDataAccess jda;
    jsonData = jda.load(reply);

    qDebug() << "Received JSON data: " << jsonData;

    QVariant result = jsonData.value<QVariantMap>();
    QString addressTitle = result.toMap().value("address").toMap().value("title").toString();

QObject* webViewAsQObject = root->findChild<QObject*>(
        QString("imprintWebViewObj"));
if (webViewAsQObject) {
    WebView* webView = qobject_cast<bb::cascades::WebView*>(
            webViewAsQObject);

    QString html = QString("<h1>foo</h1>");
    qDebug() << "Loading html into webview: " << html;
    webView->setHtml(html);
}
}

And this is the qml file:

import bb.cascades 1.2

Page {
    Container {
        layout: DockLayout {
        }
        Container {

            Container {
                leftPadding: 20
                topPadding: 30
                bottomPadding: 30
                Label {
                    text: "Impressum"
                    textStyle.fontFamily: "Georgia"
                    textStyle.fontSize: FontSize.Large
                }

                ScrollView {
                    Container {
                        background: Color.create("#f8f8f8")
                        layout: StackLayout {
                            orientation: LayoutOrientation.TopToBottom
                        }
                        WebView {
                            id: imprintWebView
                            objectName: "imprintWebViewObj"   
                        }

                    }

                }

            }

        } // Container Content end


    }

}
Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
  • Could you add the associated QML? – Konrad Lindenbach Sep 17 '13 at 18:36
  • Of course, edited my original post. Actually, it worked once a moment ago. After building again, it did not. I thought it had something to do with the `objectName` and it had to be the same as `id` but this is not the case with the MapView I use in another qml. – Sebastian Wramba Sep 18 '13 at 09:27
  • Have you considered that your HTML is poorly formatted? I.e. missing a tag? – Konrad Lindenbach Sep 18 '13 at 20:44
  • Yes, I shortened it for this question. But even with properly formatted HTML, it does not work. I'll edit my question. I added a log statement which is executed, but the `WebView` does not change. – Sebastian Wramba Sep 19 '13 at 08:22
  • I had to exchange the `WebView` with other native components. Now it works only the first time after I launch the app. It loads the JSON and fills in the labels. Then I `pop` the page from the navigation pane and `push()` it again and it leaves the labels ontouched. – Sebastian Wramba Sep 20 '13 at 09:26

1 Answers1

0

I solved the problem. There were two problems not directly associated with the threading but with how I handle the pages in the navigation pane stack.

I have a Label that pushed the page in an onTouch event. Unfortunately, I did not check for the TouchEvent.UP. This caused the page pushed to be twice, but the labels only changed on the first page. Thus, the correct code is now:

                Label {
                    text: "Imprint"
                    onTouch: {
                        if (event.touchType == TouchType.Up) {
                            navigationPane.push(imprintDefinition.createObject());
                            _main.loadImprint();
                        }
                    }
                }

Second, although I popped the page, the objects were still in memory. So, when I did root->findObject() it found an "old" object with that object name, but this was not the one visible on the screen.

I solved this by adding this to the navigation pane, as described in the BlackBerry documentation (http://developer.blackberry.com/native/documentation/cascades/ui/navigation/multiple_screens_stack.html):

NavigationPane {
    id: navigationPane

    Page {
     // ...
    } // Page

    onPopTransitionEnded: {
        page.destroy();
    }
}
Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58