简体   繁体   English

防止Qt窗口在挂钩的应用程序中关闭,Eventfilter不会执行任何操作

[英]Preventing Qt window from closing in hooked application, Eventfilter does nothing

I have hooked an application, which uses Qt. 我已经迷上了一个使用Qt的应用程序。 The application often shows popup windows, when the popups are closed, the parent window of the popup dialog also gets closed. 该应用程序通常显示弹出窗口,当弹出窗口关闭时,弹出对话框的父窗口也会关闭。

I have written an EventFilter which is supposed to prevent the parent windows from closing. 我写了一个EventFilter,它应该防止父窗口关闭。 I can see in the debugger, that the EventFilter is called, but the windows close anyway. 我可以在调试器中看到EventFilter被调用了,但是窗口还是关闭了。

This is the filter: 这是过滤器:

bool CloseEventFilter::eventFilter(QObject* object, QEvent* event){
printf("%s\n", parseEvent(event).c_str());
if(event->type() == QEvent::Close){
    event->accept();
    return true;
}
if(event->type() == QEvent::Hide){
    event->accept();
    return true;
}
if(event->type() == QEvent::HideToParent){
    event->accept();
    return true;
}
if(event->type() == QEvent::Destroy){
    event->accept();
    return true;
}
if(event->type() == QEvent::DeferredDelete){
    event->accept();
    return true;
}
if(event->type() == QEvent::ChildRemoved){
    event->accept();
    return true;
}
return false;

} }

Is there anytrhing wrong with the filter? 过滤器有问题吗? Are the other ways to do it? 还有其他方法吗?

The CloseEvent is not an event that is processed by closing the window. CloseEvent不是通过关闭窗口处理的事件。 Closing the window happens after the event is fired at the point where it was fired. 事件被触发后,将在事件触发后关闭窗口。 Therefore accepting the event in a filter might stop it from propagating, but not stop the window closing. 因此,在过滤器中接受事件可能会阻止其传播,但不会阻止窗口关闭。

When catching a CloseEvent , you are having a chance to ignore the event instead of accepting it. 捕获CloseEvent ,您有机会忽略事件而不是接受事件。 The window closing will only happen if the event was accepted, which is the default. 仅当事件被接受时,窗口才会关闭,这是默认设置。

The isAccepted() function returns true if the event's receiver has agreed to close the widget; 如果事件的接收者已经同意关闭小部件,则isAccepted()函数将返回true;否则,此函数将返回true。 call accept() to agree to close the widget and call ignore() if the receiver of this event does not want the widget to be closed. 调用accept()同意关闭小部件,如果此事件的接收者不希望关闭小部件,则调用ignore()。

This means that in your code you need to call event->ignore() instead of event->accept() . 这意味着在代码中,您需要调用event->ignore()而不是event->accept()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM