QML Global key monitoring, interception

Source: Internet
Author: User

Recently tried to use Qt to do Android applications, all the way, the ground is a pit, but the application of the development is not complicated, the pit is not deep, all step by step, but a problem can not solve the--back button return function, but today finally solved ...

Used Qml know, in the QML to handle the keys must use the keys attached property, but also to set the focus property of the element is true, and once the focus becomes false (such as jumping to other interfaces, etc.), the key message can no longer be processed.

In Qt fot Android, the default implementation of the Back button is to exit the program directly, and my application uses the StackView for interface jumps, I want back to implement the return function, and exit the app with an exit prompt to confirm, rather than just exit. However, after the jump interface and other operations, the original Processing button item is easy to lose focus, so that a press the back button to exit directly.

So there is no global monitoring of the key message, by blocking the back button message to steady, once and for all to implement the return function? The solution is actually very simple, in fact, qml many classes have corresponding C + + class, such as I use the Applicationwindow, corresponding C + + class is Qquickwindow, since it is so good to run. As we know, in QT C + + processing the key time is not so many limitations and concerns, just overload the key processing function on the line. So I think, qml the key events are passed from the parent to the child, that if I in the topmost window class Applicationwindow to intercept the key message, do not you? It turns out that it is indeed possible!

The specific implementation steps are as follows:

First, implement a simple filter class. This class must inherit from Qobject or its subclasses, and we only need to overload a virtual function eventfilter, see QT related documentation for the use of event filters. In EventFilter we can pick up the key messages we want, such as the back button messages I need, and all the other releases. In addition I have defined two signals for the intercepted Back button message: backkeypressed and backkeyreleased.

After implementing the filter, create the instance and register it with the QML attribute: Engine.rootcontext ()->setcontextproperty (...) so that we can directly connect the back button signal in the filter in the QML.

Next, find the top-level window instance in C + + code. To use Qqmlapplicationengine and Applicationwindow as an example, first set the top-level window object Name (objectname property) in QML. Then call Qqmlapplicationengine's Rootobjects method to find the top-level window instance by enumerating the return values against the object name.

And then, of course, installing our filter instance in the window instance, yes, that's the instance in the previous book to the QML attribute, and all the key events (and of course other events) are filtered out before being processed.

Finally, we only need to connect the backkeypressed and backkeyreleased signals in the QML to handle the back button, and no longer afraid to come back ...

This article is from the "Binary Art" blog, so be sure to keep this source http://bran101.blog.51cto.com/2422138/1825258

QML Global key monitoring, interception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.