开发者

Keyboard- and mouse-events transparent widget

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 h开发者_如何学编程ere 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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜