I am creating a small Android application in Qt 5.10. The application consists of a main.qml that holds a Qt StackView. In this StackView one Qt Flickable page is defined with a button on it to navigate to a new page, which is a Qt WebView. After navigation, this WebView will load a local HTML page. But when I hit the Android back button on the second page the app will not navigate back to the first one. See the code below for more information.
main.qml
import QtQuick 2.10
import QtQuick.Controls 2.2
ApplicationWindow {
id: appWindow
visible: true
StackView {
id: navPane
anchors.fill: parent
initialItem: pageOne
Keys.onBackPressed: {
popOnePage()
}
function popOnePage() {
if(navPane.depth == 1)
return
pop()
}
function pushOnePage(pageComponent) {
push(pageComponent)
}
}
Component {
id: pageOne
PageOne {}
}
Component {
id: pageTwo
PageTwo {}
}
}
import QtQuick 2.10
import QtQuick.Controls 2.2
ApplicationWindow {
id: appWindow
visible: true
StackView {
id: navPane
anchors.fill: parent
initialItem: pageOne
Keys.onBackPressed: {
popOnePage()
}
function popOnePage() {
if(navPane.depth == 1)
return
pop()
}
function pushOnePage(pageComponent) {
push(pageComponent)
}
}
Component {
id: pageOne
PageOne {}
}
Component {
id: pageTwo
PageTwo {}
}
}
To copy to clipboard, switch view to plain text mode
PageOne.qml
import QtQuick 2.10
import QtQuick.Controls 2.2
Flickable {
Button {
text: "Go to page 2"
anchors.centerIn: parent
onClicked: {
navPane.pushOnePage(pageTwo)
}
}
}
import QtQuick 2.10
import QtQuick.Controls 2.2
Flickable {
Button {
text: "Go to page 2"
anchors.centerIn: parent
onClicked: {
navPane.pushOnePage(pageTwo)
}
}
}
To copy to clipboard, switch view to plain text mode
PageTwo.qml
import QtQuick 2.10
import QtWebView 1.1
WebView {
id: webView
url: initialUrl //link to local HTML file
}
import QtQuick 2.10
import QtWebView 1.1
WebView {
id: webView
url: initialUrl //link to local HTML file
}
To copy to clipboard, switch view to plain text mode
After some debugging I found out that the Keys.onBackPressed signal is never received. But when I use a Flickable instead of a WebView the signal will be received and the navigation works as expected. Can someone tell me if it is possible to receive this signal while using a WebView or maybe tell me what I am doing wrong? It will also do for me if there exists another component that can work with HTML. Any help would be greatly appreciated!
Bookmarks