我在 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/