问题描述
我正在使用 Qt5.3 和 Qtquick2.1 创建一个程序.我正在尝试使用 Keys.onReleased 在我的代码中捕获 android 上的后退按钮按下.但是该事件没有被触发.此外,我已将项目焦点设置为 true.但仍然没有成功.这是代码示例
I am creating a program in Qt5.3 and Qtquick2.1. I am trying to capture back button press on android in my code using Keys.onReleased. But that event is not getting triggered. Also I have set the item focus to true. But still no success. Here is the code sample
import QtQuick 2.1
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Layouts 1.1
import QtQuick.Window 2.1
Rectangle
{
id: main2
focus: true
width: Screen.Width
height: Screen.Height
Keys.enabled: true
Keys.priority: Keys.BeforeItem
property string load_page: ""
signal deskConnected()
Loader{
id: pageloader
anchors.fill: parent
source: "qrc:/qml/resources/Firstpage.qml"
}
onDeskConnected: {
pageloader.item.onDeskConnected()
}
function loadPatwin(){
pageloader.source = "qrc:/qml/resources/Secondpage.qml";
}
Keys.onReleased: {
console.log("back");
if (event.key === Qt.Key_Back) {
event.accepted=true;
}
}
}
这里的 loadPatwin 是在按下某个其他 qml 中定义的按钮时调用的函数.并加载一个新的 qml.但在那之后,当我按下 android 上的后退按钮时,应用程序被关闭,它甚至不会在日志中打印后退".有什么建议我在这里做错了吗?
Here loadPatwin is the function which gets called on pressing a button which is defined in some other qml. And loads a new qml. But after that when I am pressing the back button on android, the app gets closed and it doesn't print even "back" in the logs. Any suggestions what I am doing wrong here?
提前致谢.
推荐答案
在qt quick(Qt5.1及更高版本)中,ApplicationWindow
和Window
,都有一个<当用户在 android 中触摸后退按钮时发出的 code>closure 信号.您可以简单地实现一个 onClosing 处理程序并将 close 参数设置为 false.操作方法如下:
In qt quick (Qt5.1 and later) the ApplicationWindow
and Window
, both have a closing
signal that is emitted when the user touches the back button in android.
You can simply implement an onClosing handler and set the close parameter to false. Here is how to do it:
onClosing: {
close.accepted = false
}
通过这个处理程序,您可以轻松管理 android 设备的后退按钮.它不需要任何额外的许可.例如,假设您有一个 StackView 来管理 GUI.您可以编写这些代码行,然后您的应用程序的行为就像原生 android 应用程序一样:
By this handler you can easily manage the back button of android device. It does not need any extra permission. For example suppose you have a StackView to manage the GUI. You can write these lines of code, Then your application behaves as like as a native android app:
onClosing: {
if(stack.depth > 1){
close.accepted = false
stack.pop();
}else{
return;
}
}
这篇关于Android后退按钮按下不会触发keys.onreleased qml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!