键盘和鼠标事件透明小部件

发布于 2024-08-17 03:01:37 字数 195 浏览 2 评论 0原文

当我单击主窗口中的按钮时,我希望它对键盘和鼠标事件变得透明,即所有键盘和鼠标事件都应传递到其下方的任何窗口,就好像该窗口不存在一样。

Qt::WA_TransparentForMouseEvents 在这里不起作用,因为我猜这只会使子窗口对键盘和鼠标事件透明。我的窗口是主窗口,我想将所有事件传递到桌面上的任何窗口而不仅仅是父窗口。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

述情 2024-08-24 03:01:37

这是示例代码,它使我能够进行绘图,并且鼠标事件仍然可以通过它。

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

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();
}
吃→可爱长大的 2024-08-24 03:01:37

我一直在我的应用程序中使用 Qt::WA_TransparentForMouseEvents 并且效果很好。

我不明白你面临的问题,但它应该有效。如果您仍然遇到问题,请将属性设置为 Qt::WA_TransparentForMouseEventsQt::WA_Translucentbackground

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文