在Qt中,我如何忽略所有鼠标和键盘事件,然后停止忽略它们?那就是:点击一个按钮,忽略孩子们的所有事件; 再次点击,不要忽略.明白了吗?我有以下几行,但也许我做错了什么:
setAttribute(Qt::WA_TransparentForMouseEvents);
setFocusPolicy(Qt::NoFocus);
Run Code Online (Sandbox Code Playgroud)
小智 11
不要使用setFocusPolicy(Qt :: NoFocus); 它会将事件传播给父母.仅使用setAttribute(Qt :: WA_TransparentForMouseEvents);
您可以在鼠标和键盘事件上使用事件过滤器,在需要时过滤一些按键或鼠标点击:
yourWidget->installEventFilter(this);
Run Code Online (Sandbox Code Playgroud)
...
bool YourFrm::eventFilter(QObject* pObject, QEvent* pEvent)
{
if (pEvent->type() == QEvent::KeyPress)
{
QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>(pEvent);
int PressedKey = pKeyEvent->key();
if(PressedKey == Qt::Key_Return)
{
// Filter Return key....
return true;
}
// standard event processing
return QObject::eventFilter(pObject, pEvent);
}
else if (pEvent->type() == QEvent::MouseButtonPress)
{
QMouseEvent* pMouseEvent = static_cast<QMouseEvent*>(pEvent);
... // etc...
}
else
{
// standard event processing
return QObject::eventFilter(pObject, pEvent);
}
}
Run Code Online (Sandbox Code Playgroud)
更多信息:http://qt.nokia.com/doc/4.6/eventsandfilters.html
希望能帮助到你 !
您的意思是 QGraphicsItem 吗?
如果是,您可以致电
void QGraphicsItem::setEnabled ( bool enabled )
Run Code Online (Sandbox Code Playgroud)
并且要稍后激活事件,因为该项目不再接收事件,您必须经过场景,因为您无法直接接收项目上的事件。
如果您的问题不是使用 GraphicsView Frameworks,而是使用 qt 的其他部分,则几乎是相同的过程:
您可以调用:
QWidget::setEnabled(false) //like Massimo said
Run Code Online (Sandbox Code Playgroud)
为了响应小部件,只需检测应用程序中对象内的按下事件即可在小部件上调用“setEnable(true)”!
希望能帮助到你 !`