如何接受/忽略QKeyEvent [英] how to accept/ignore QKeyEvent

查看:149
本文介绍了如何接受/忽略QKeyEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://qt-project.org/doc/qt- 5/qwidget.html#keyPressEvent

请注意,QKeyEvent以isAccepted()== true开头,因此您不必 需要调用QKeyEvent :: accept()-只是不调用基类 如果您对密钥采取行动,就可以实现.

Note that QKeyEvent starts with isAccepted() == true, so you do not need to call QKeyEvent::accept() - just do not call the base class implementation if you act upon the key.

http://qt-project.org/doc/qt- 5/qkeyevent.html#details

按键事件包含一个特殊的接受标志,该标志指示是否 接收者将处理关键事件.如果 小部件不处理按键或释放事件.关键事件 沿父窗口小部件链向上传播,直到一个窗口小部件接受它为止 与accept()或事件过滤器一起使用它.的关键事件 默认情况下,将忽略多媒体键.如果您应该调用accept() 您的小部件会处理这些事件.

A key event contains a special accept flag that indicates whether the receiver will handle the key event. You should call ignore() if the key press or release event is not handled by your widget. A key event is propagated up the parent widget chain until a widget accepts it with accept() or an event filter consumes it. Key events for multimedia keys are ignored by default. You should call accept() if your widget handles those events.

这有点令人困惑.我应该打电话给接受还是不接受?如果不处理,应该调用基本实现还是调用ignore?

That's a bit confusing. Should I call accept or not? Should I call the base implementation or call ignore if I don't handle it?

第一页显示我不需要调用accept(),第二页显示为在父窗口小部件链中传播,直到窗口小部件使用accept()接受它为止"

The first page reads I don't need to call accept(), the second reads "is propagated up the parent widget chain until a widget accepts it with accept()"

如果我调用基本版本,如果它是QWidget,它将基本上调用ignore().那不是意味着它会返回到我的keyPressedEvent并以忽略状态返回,然后再次调用QWidget的版本,原因是关键事件被传播到父窗口小部件"?

If I call the base version it'll basically call ignore() if it's QWidget. Wouldn't that mean it would return to my keyPressedEvent, return with ignored state, then QWidget's version is called again cause "key event is propagated up to the parent widget"?

推荐答案

就像说的那样,它会被自动接受.因此,如果您要处理它,则无需执行任何操作.仅当您不处理按键事件时,才应调用ignore().

Like it says, it is accepted automatically. So if you're handling it, you don't have to do anything. Only if you're not handling the key event you should call ignore().

如果您的类是QWidget的子类,并且正在处理键事件,则不要调用基本实现.如果您不处理,请您可以调用基本实现,因为它将调用ignore() .

If your class is a subclass of QWidget, and if you are handling the key event, then do not call the base implementation. If you don't handle it, you can just call the base implementation as it will call ignore().

如果您想知道,可以阅读有关接受和忽略事件的更多信息.

You can read this, if you want to know more about accepting and ignoring events.

  • 第一页显示我不需要调用accept()",第二页显示在父窗口小部件链中传播,直到窗口小部件使用accept()接受它"

这意味着,如果您选择 忽略 ,它将被传播到parent widget.如果您选择 接受 ,它将 传播到parent widget.

This means if you choose to ignore it, it will be propagated to the parent widget. If you choose to accept it, it will not be propagated to the parent widget.

  • 如果我调用基本版本,则如果它是QWidget,它将基本上调用ignore().那不是意味着它将返回到我的keyPressedEvent,并以忽略状态返回,然后再次调用QWidget的版本,原因是键事件传播到了父窗口小部件"?

基类与父窗口小部件不同.基类是您的类的派生类.父窗口小部件是一个包含this对象作为其子窗口小部件的对象.这通常是作为参数传递给构造函数的小部件.但是,如果父窗口小部件是QWidget类型,则是的.

The base class is not the same as the parent widget. Base class is the class your class is derived from. A parent widget is an object that contains this object as its child widget. This is usually the widget that is passed to your constructor as an argument. But if the parent widget is a QWidget type, then yes, that would be the case.

这篇关于如何接受/忽略QKeyEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆