Qt – Keyboard- and mouse-events transparent widget

event handlingmouseqttransparency

When I click a button my main window I want it to become transparent to keyboard and mouse events, i.e. all keyboard and mouse events should pass to any windows below it as if that window is not present there.

Qt::WA_TransparentForMouseEvents does not work here as this only make child windows transparent to keyboard and mouse events I guess. And my window is main window and I want to pass all event to any window on desktop not just parent window.

Best Answer

Here is the sample code which enables me to do drawing on and still mouse events go through it.

Sample::Sample(QWidget *pParent):QWidget(pParent)
{
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_TransparentForMouseEvents);
    setWindowFlags(Qt::FramelessWindowHint);
    QDesktopWidget qDesktopWidget;
    QRect screenSize = qDesktopWidget.screenGeometry();
    setGeometry(screenSize);
}

Sample::~Sample()
{
}

void Sample::paintEvent(QPaintEvent*)
{
    QDesktopWidget new QDesktopWidget();
    QRect rectangle = qDesktopWidget->screenGeometry();
    setGeometry(rectangle);

    const QPoint points[5] = {
        QPoint(0, 20),
        QPoint(rectangle.width(), 20),
        QPoint(rectangle.width(), rectangle.height()),
        QPoint(0,rectangle.height()),
        QPoint(0, 0)
    };

    QPen pen(Qt::blue, 10, Qt::SolidLine, Qt::SquareCap);
    QPainter painter(this);
    painter.setPen(pen);
    painter.drawPolyline(points, 5);
    painter.end();
}