我从带有QNetworkRequest
的Web服务加载JSON,然后将其放入JsonDataAccess
对象中。我可以解析JSON,一切都很好。
但是我想将JSON字符串格式化为HTML并将其显示在WebView中,这似乎与finished(QNetworkReply*)
信号使用的线程无关,因为它应该是非UI阻塞的。
但是,我可以使用相同的方法将标记添加到MapView
。但更新WebView
不起作用。如何进入主线程来更新WebView?
这是我目前使用的方法(插槽):
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);
}
}
这是qml文件:
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
}
}
答案 0 :(得分:0)
我解决了这个问题。有两个问题与线程没有直接关联,而是我如何处理导航窗格堆栈中的页面。
我有Label
在onTouch
事件中推送了该页面。不幸的是,我没有检查TouchEvent.UP
。这导致页面被推送两次,但标签仅在第一页上更改。因此,现在正确的代码是:
Label {
text: "Imprint"
onTouch: {
if (event.touchType == TouchType.Up) {
navigationPane.push(imprintDefinition.createObject());
_main.loadImprint();
}
}
}
其次,虽然我弹出了页面,但对象仍在内存中。所以,当我root->findObject()
时,它找到了一个带有object name
的“旧”对象,但这不是屏幕上可见的对象。
我通过将其添加到导航窗格解决了这个问题,如BlackBerry文档(http://developer.blackberry.com/native/documentation/cascades/ui/navigation/multiple_screens_stack.html)中所述:
NavigationPane {
id: navigationPane
Page {
// ...
} // Page
onPopTransitionEnded: {
page.destroy();
}
}