Important: Please read the Qt Code of Conduct -

Translucent window - mouse event

  • Hello,
    I have translucent widget ( Qt::WA_TranslucentBackground) and I want to catch mouse press events on that widget.
    The widget is top level and I know that on Win and Mac OS I could use hook but I would prefer not to.

    Any advice are more then welcome.

    Best regards.

  • Did you try overriding mousePressEvent() and did you set the Qt::FramelessWindowHint flag?

  • I used event filter to do so, and checking mouse events for scene and view and the problem is that if I press on transparent area then app. don't receive mouse events, when hit on already created item then I mouse event is catch.

    On another est app I did used view mouse events, same situation.

    Update: translucent widgets are QGraphicsView and QGraphics scene.

  • Instead of setting Qt::WA_TranslucentBackground you can use setBackgroundRole(QPalette::NoRole) to make the background transparent. Then all mouse events are handled as usual.

  • With setBackgroundRole background is not transparent in scene.

  • Situation looks like this:

    OS: Win 8, Qt 5.2.0, 5.2.1, 5.1.1 etc. same story.

    When creating translucent window and clicking on translucent/transparent part of application mouse events are not catch by application:


    Above example illustrate situation, when clicking on window test is selected.
    What i'm looking is solution that would allow me to choose if app want's to process event, if yes then mouse press is consumed by application if no then mouse press goes to OS, and in above case text cursor is positioned on mouse position

    Any suggestions are more then welcome.

  • setattribute to Qt::WA_TransparentForMouseEvents and Qt::WA_Translucentbackground.

    I hope it will solve you problem.

  • Hmm, it seems WA_TransparentForMouseEvents only works if it is not most parent widget. When adding a pushButton with that attribute set, the events are aways caught by the mainwindow, regardless if I click the button or the transparent mainwindow.

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    setAttribute(Qt::WA_TranslucentBackground, true);
    setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->pushButton->setAttribute(Qt::WA_TransparentForMouseEvents, true);


    delete ui;

    void MainWindow::mousePressEvent(QMouseEvent *)
    qDebug() << "pressed";

Log in to reply