简体   繁体   中英

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

I have hooked an application, which uses 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. I can see in the debugger, that the EventFilter is called, but the windows close anyway.

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. 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. 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; 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.

This means that in your code you need to call event->ignore() instead of event->accept() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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