简体   繁体   English

Qt QML - 处理导航按钮?

[英]Qt QML - handle navigation buttons?

Context:语境:

Qt QML 5.6 Qt QML 5.6

I could not find related documentation to handle the Android navigation buttons in my QML app (triangle, square and circle, at the bottom of the screen).我找不到相关文档来处理我的 QML 应用程序中的 Android 导航按钮(屏幕底部的三角形、正方形和圆形)。

Currently, when touching them, it just puts my app in the background.目前,当触摸它们时,它只是将我的应用程序置于后台。 I would like to give them some logic.我想给他们一些逻辑。

Question:问题:

Is it possible to manage those buttons in QML?是否可以在 QML 中管理这些按钮? Or will I have to deal with a c++ event handler?还是我必须处理 C++ 事件处理程序? (If so, which code should one look after?) (如果是这样,应该注意哪个代码?)

Thanks谢谢

Poor man's solution:穷人的解决方法:

Within the Window or application window scope, use在 Window 或应用程序窗口范围内,使用

    onClosing: {
      do_what_you_need()
      close.accepted = false
    }

In do_what_you_need() , you may call Qt.quit if it is ok.do_what_you_need()中,如果可以,您可以调用 Qt.quit。

It is possible to manage these buttons from the QML.可以从 QML 管理这些按钮。 In QML, those key presses are handled exactly like key presses on a keyboard.在 QML 中,这些按键的处理方式与键盘上的按键完全一样。 For example, Qt.Key_Back refers to the back key(triangle) and Qt.Key_Home refers to the home key(square).例如, Qt.Key_Back指的是返回键(三角形), Qt.Key_Home指的是主键(正方形)。 Here is an example of listening for the home key in QML:下面是一个在 QML 中监听 home 键的例子:

Keys.onPressed: {
   if (event.key == Qt.Key_Home) {
      console.log("Square button(home) pressed");
   }
}

For more on the key enumerations in Qt, see this documentation: http://doc.qt.io/qt-5/qt.html#Key-enum有关 Qt 中关键枚举的更多信息,请参阅此文档: http ://doc.qt.io/qt-5/qt.html#Key-enum

For completeness, example with ignoring the back button (do not minimize the app)为了完整起见,例如忽略后退按钮(不要最小化应用程序)

focus: true
Keys.onPressed: {
   if (event.key == Qt.Key_Back) {
      event.accepted = true
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM