1
votes

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.

2

2 Answers

2
votes

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();
}
2
votes

I have been using Qt::WA_TransparentForMouseEvents in my application and it works great.

I don't understand the problem you are facing but it should work. If you still have problem setattribute to Qt::WA_TransparentForMouseEvents and Qt::WA_Translucentbackground.