Get mouse event for widget with trasparent background

  • Hello.

    Is there a way to get mouse event in widget with transparent background, if mouse cursor is under transparent part of widgets.

    I have main Qt window, this window has one child, which is third-party non-Qt window. Above i have panel (QWindow), which is owned by main window. This panel has Qt::Window flag and parent is main widnow.

    My panel have attributes:

    setAttribute(Qt::WA_NoSystemBackground, true);
    setAttribute(Qt::WA_TranslucentBackground, true);

    On panel i have non-trasparent child widgets. I able to get mouse events only if mouse cursor is under child widget. I know if my transparent panel did not get event, a widget under it get event. But under my widget third-party window is located. I cannot modify his code. My goal to intercept mouse event and implement drag&drop and third-party window should not get mouse event in this case.

    I tested under Windows Qt 5.3.

    Under mac all mouse events under transparent part of panel i can get in main window mouse event function. It does not work the same under Windows.

  • Qt Champions 2017

    You can have a go with event filter and see what flows around of events.

    bool MainView::eventFilter(QObject *obj, QEvent *event)
    if (event->type() == QEvent::MouseButtonPress)
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        int mx=mouseEvent->pos().x(),my=mouseEvent->pos().y();
        qDebug() << QString::number(mx)<<" "<<QString::number(my);

  • You can have a go with event filter and see what flows around of events.

    Unfortunately it does not work. Also i tried to use nativeEvent method, but it does not work too.

  • Qt Champions 2017

    Ok but what did you learn?
    I never tested what WA_TranslucentBackground will do to the event flow but
    an event filer would cast some light over that.
    So such windows do not get any mouse events?

  • Looks like my third-party window gets mouse event first and process it. And event filter is not called for mouse events for transparent parts of my panel.

    I learned MSDN and looks like it is feature of Windows: [](link url)

    Hit testing of a layered window is based on the shape and transparency of the window. This means that the areas of the window that are color-keyed or whose alpha value is zero will let the mouse messages through.
    If the layered window has the WS_EX_TRANSPARENT extended window style, the shape of the layered window will be ignored and the mouse events will be passed to the other windows underneath the layered window.

    But i will investigate more and post message if i find solution. Maybe i can use event hook.

  • I found one solution, which works for me. But it looks like a hack :)

    I added child QWidget to my panel. I added QGraphicsOpacityEffect for this QWidget and set opacity value to 0.01. As the results widget is invisible, but my panel is started to get mouse events. One remark: QWidget should fill full panel.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.