Android后退按钮不会触发keys.onreleased qml

Ama*_*neh 6 qt android qml qt5 qtquick2

我正在Qt5.3和Qtquick2.1中创建一个程序.我试图使用Keys.onReleased在我的代码中捕获按钮上的按钮.但是这个事件没有被触发.此外,我已将项目焦点设置为true.但仍然没有成功.这是代码示例

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里loadPatwin是在按下某个其他qml中定义的按钮时调用的函数.并加载一个新的qml.但在那之后,当我按下Android上的后退按钮时,应用程序将关闭,它甚至不会在日志中"打印".我在这里做错了什么建议?

提前致谢.

a.t*_*aby 15

在Qt快速(Qt5.1和更高版本)ApplicationWindowWindow,两者都具有closing当用户在机器人触摸返回按钮时发射信号.您可以简单地实现onClosing处理程序并将close参数设置为false.这是怎么做的:

onClosing: {
    close.accepted = false
}
Run Code Online (Sandbox Code Playgroud)

通过这个处理程序,您可以轻松管理Android设备的后退按钮.它不需要任何额外的许可.例如,假设您有一个StackView来管理GUI.您可以编写这些代码行,然后您的应用程序就像本机Android应用程序一样:

onClosing: {
        if(stack.depth > 1){
            close.accepted = false
            stack.pop();
        }else{
            return;
        }
    }
Run Code Online (Sandbox Code Playgroud)


小智 5

通过在项目加载完成后添加"forceActiveFocus()",它对我有用.

在你的例子中,我会把它放在开头.像这样:

Rectangle
{
    id: main2
    focus: true

    Component.onCompleted: {
        main2.forceActiveFocus()
    }
Run Code Online (Sandbox Code Playgroud)


小智 1

我不知道这是否是一个很好的例子,但我曾经从 QGuiApplication 创建自己的 GuiApplication 类子类

#ifndef GUIAPPLICATION_H
#define GUIAPPLICATION_H

#include <QGuiApplication>

class GuiApplication : public QGuiApplication
{
    Q_OBJECT
public:
#ifdef Q_QDOC
    explicit GuiApplication(int &argc, char **argv);
#else
    explicit GuiApplication(int &argc, char **argv, int = ApplicationFlags);
#endif

    bool notify(QObject *receiver, QEvent *event);
signals:
    void back();

};

#endif // GUIAPPLICATION_H
Run Code Online (Sandbox Code Playgroud)

这是cpp代码

#include "guiapplication.h"
#include <QDebug>

GuiApplication::GuiApplication(int &argc, char **argv, int) :
    QGuiApplication(argc, argv)
{
}

bool GuiApplication::notify(QObject *receiver, QEvent *event)
{
// to intercept android's back button
#ifdef Q_OS_ANDROID
    if(event->type() == QEvent::Close) {
        emit back();
        return false;
    }
    else {
        return QGuiApplication::notify(receiver,event);
    }
#else
        return QGuiApplication::notify(receiver,event);
#endif
}
Run Code Online (Sandbox Code Playgroud)

对于main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "guiapplication.h"

int main(int argc, char *argv[])
{
//    QGuiApplication app(argc, argv);
    GuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QQmlContext *rootContext = engine.rootContext();
    rootContext->setContextProperty("GUI", &app);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

最后是 main.qml

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    id: applicationWindow

    visible: true
    width: 360
    height: 360

    Connections {
        target: GUI
        onBack: console.log("back")
    }
}
Run Code Online (Sandbox Code Playgroud)