Detect if the mouse is clicked outside GroupBox

纵饮孤独 提交于 2019-12-25 09:27:29

问题


I am looking for an event if the mouse is clicked outside of the groupBox in Qt. I tried FocusOutEvent but was not able to get the event:

ui.groupBox->installEventFilter(this); 

void myClass::focusOutEvent(QFocusEvent *event) { ui.groupBox->hide(); }

Any kind of help would be greatly appreciated!


回答1:


You have the following options:

  1. Subclass QGroupBox and override mousePressEvent()

  2. Install an event filter on that group box and catch QMouseEvents

  3. If you want to catch only right mouse clicks (context menu), implement a custom context menu handler.




回答2:


The problem is that the events of the monitored object are not forwarded to the native event handlers of the filter object such as focusOutEvent, but to a special virtual event function, i.e. eventFilter(QObject *obj, QEvent *event) as documented in installEventFilter. So, your event handler should look like this:

bool myClass::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui.groupBox && event->type () == QEvent::FocusOut)
        ui.groupBox->hide(); 
    return false;
}


来源:https://stackoverflow.com/questions/44394284/detect-if-the-mouse-is-clicked-outside-groupbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!