c++ - QT:忽略复选框选择的关键事件

标签 c++ qt events

我在 Windows 上有一个 QT 应用程序,它有一个使用箭头键的模式,还有一个应该完全忽略这些箭头键的模式。也就是说,我希望箭头键在用户选中一个框后不触发任何事件。

我看到一篇帖子建议使用 eventFilter(),但我不知道如何使用它。这是监听用户的复选框事件,一旦用户选中它就会触发。在其他部分,我希望 eventFilter() 为箭头键工作,但到目前为止我无法让它运行。

void MainWindow::on_checkBoxSmartCutMode_stateChanged(int arg1)
{
    if (arg1 == 0)
    {
     // do as usual, arrow keys should work
    }
    else
    {
        eventFilter(); // if any arrow key is pressed, ignore the event
    }

}

有什么建议吗?

最佳答案

您可以使用 keyEvent通过覆盖 keyPressEvent 作为您的关键过滤器并测试您的复选框状态。

例子:

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    // check your checkbox state
    if (ui->poCheckBox->checkState() == Qt::Unchecked)
       // do as usual, arrow keys should work
       return;

    switch(event->key())
    {
      case Qt::Key_Left:
      case Qt::Key_Right: // add more cases as needed
        event->ignore(); // if any arrow key is pressed, ignore the event
        return;
    }

    // handle the event
}

关于c++ - QT:忽略复选框选择的关键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47388428/

相关文章:

c++ - OpenCV CMake对函数的 undefined reference

c++ - BST 以 2d 数组作为节点结构中的键而不是 int

qt - QML:无法读取未定义的属性 'xxx'

java - JavaFX 拖放事件

javascript - JS。将事件属性类型分配给变量

c++ - 调用未定义函数 'save'

c++ - 不使用模板参数的函数导致过多的代码膨胀?

c++ - 基类析构函数 C++ 中的段错误

c++ - 使用 QDir 过滤器列出子目录中的文件

c# - 将不同的枚举组合成一个参数 C#