如何禁用Qt在Linux上捕获窗口小部件焦点导航的箭头键的行为 [英] How do I disable Qt's behavior on Linux of capturing arrow keys for widget focus navigation

查看:285
本文介绍了如何禁用Qt在Linux上捕获窗口小部件焦点导航的箭头键的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个主要在MacOS上开发的Qt应用程序,然后在Linux上构建和测试。



我们定义了一个keyPressEvent )方法在我们的主窗口类中响应某些键盘事件。其中,我们回应Qt :: Key_Left,Qt :: Key_Right和Qt :: Key_Space。这在MacOS上非常出色。然而,在Linux上,我们从来没有收到这些事件。



做一些谷歌搜索(并用我们的应用程序在Linux上的行为来确认它),似乎这样做的原因是Qt使用这些键在应用程序的GUI中按钮小部件的键盘导航。如果我在Linux上按箭头键,我会循环浏览所有活动按钮小部件,依次选择每个。如果我点击空格键,按下当前选择的按钮。



Googling迄今为止我所能找到的所有内容是关于如何进行子类化或应用的建议过滤器到特定的按钮,以避免这种行为,让按钮忽略事件并传递它。但是我不想为每一个我将其插入到GUI中的按钮小部件都这样做。这是只是跛脚。



有没有办法在全球禁用此行为,并允许我的应用程序代码实际获得所有的箭头键和空格键事件?

解决方案

您可以设置添加全局事件侦听器来捕获这些事件。



在窗口中构造函数:

  QApplication :: instance() - > installEventFilter(this); 

在窗口的eventFilter方法中:

  bool MainWindow :: eventFilter(QObject * object,QEvent * event){
if(event-> type()== QEvent :: KeyPress){
QKeyEvent * key_event = static_cast< QKeyEvent *>(event);
qDebug()<< key<< key_event-> key()<<< 对象<<目的;
// ...
}
return false;
}

如果您处理了一个事件,您应该返回 true 从此函数。您可以使用对象变量来查找事件的来源。例如,它可以是一些QPushButton。如果有必要,您可以检查此按钮是否是主窗口的小孩(如果您有多个顶部窗口)。



另一种方法是完全禁用按钮的焦点。将按钮' focusPolicy 设置为 NoFocus 。然后他们不会捕获关键事件。



还可以使用空的实现子类QPushButton并重新实现 keyPressEvent p>

I'm working on a Qt app that is developed primarily on MacOS, but that is then built and tested on Linux as well.

We define a keyPressEvent() method in our main window class to respond to certain keyboard events. Among others, we respond to Qt::Key_Left, Qt::Key_Right and Qt::Key_Space. This works great on MacOS. On Linux, however, we never get these events.

Doing some Googling (and confirming it with our app's behavior on Linux), it seems that the reason for this is that Qt uses these keys for keyboard navigation of button widgets in the application's GUI. If I press the arrow keys on Linux, I cycle through all the active button widgets, selecting each on in turn. If I click the spacebar, the currently selected button is pressed.

All I've been able to find so far by Googling is suggestions as to how to either subclass or apply filters to specific buttons to avoid this behavior by having the button ignore the event and pass it along. But I don'w want to have to do this for every button widget I ever put into my GUI. That's just lame.

Is there a way to disable this behavior globally and allow my app code to actually get ALL of the arrow key and spacebar events?

解决方案

You can set add global event listener to catch these events.

In the window constructor:

QApplication::instance()->installEventFilter(this);

In the window's eventFilter method:

bool MainWindow::eventFilter(QObject *object, QEvent *event) {
  if (event->type() == QEvent::KeyPress) {
    QKeyEvent* key_event = static_cast<QKeyEvent*>(event);
    qDebug() << "key" << key_event->key() << "object" << object;
    //...
  }
  return false;
}

If you have processed an event, you should return true from this function. You can use object variable to find out the source of event. For example, it can be some QPushButton. You can check if this button is a child of your main window if it's necessary (if you have multiple top windows).

Another way is to disable focus for buttons completely. Set buttons' focusPolicy to NoFocus. Then they will not catch key events.

Also you can subclass QPushButton and reimplement keyPressEvent with empty implementation.

这篇关于如何禁用Qt在Linux上捕获窗口小部件焦点导航的箭头键的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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