QQuickView (QML) 对鼠标事件透明 [英] QQuickView (QML) transparent for mouse events

查看:93
本文介绍了QQuickView (QML) 对鼠标事件透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大矩形,按钮居中.我希望我的矩形对鼠标事件是透明的,除了按钮,它必须是可点击的.我的意思是,我希望能够用鼠标选择矩形下的代码,就像没有显示矩形一样.

I have a big rectangle with a button centered. I would like that my rectangle is transparent to mouse events except for the button, which must be clickable. I mean, I would like to be able to select code under my rectangle with the mouse, exactly as if no Rectangle was displayed.

我为所有大 Rect 添加了一个 MouseArea,试图忽略鼠标事件,但它不起作用.

I have added a MouseArea for all the big Rect, trying to ignore mouse events, but it does not work.

我读到 'Qt::WA_TransparentForMouseEvents' 用于此目的,但在 Qt windows 中,据我所知,在我的情况下不可用.

I read that 'Qt::WA_TransparentForMouseEvents' is used for that purpose, but in Qt windows as fasr as I know, not available in my case.

提前致谢

我的 QML 是从 main.cpp 加载的:

My QML is loaded from main.cpp:

   QQuickView* pView = new QQuickView();

    pView->setSource(QUrl("qrc:/MyRect.qml"));
    pView->setFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    pView->setColor("transparent");
    pView->show();

MyRect.qml:

MyRect.qml:

import QtQuick 2.0
import QtQuick.Controls 1.4

Rectangle {
    width: 500
    height: 500

    color: "green" // it would be transparent
    opacity: 0.5

    Button {
        anchors.centerIn: parent
        height: 50; width: 50
        onClicked: console.log("clicked");
    }

    MouseArea {
        anchors.fill: parent
        enabled: false
        propagateComposedEvents: true
        hoverEnabled: false

        // All this code I think is useless...
        onClicked: mouse.accepted = false
        onReleased: mouse.accepted = false
        onEntered: mouse.accepted = false
        onExited:  mouse.accepted = false
        onWheel:  mouse.accepted = false
    }
}

推荐答案

Rectangle 默认对鼠标点击是透明的.如果你拿掉那个 MouseAreaButton 将接收点击,并且 Rectangle 上的所有点击都将传递到它的封闭父级:>

The Rectangle is transparent to mouse clicks by default. If you take away that MouseArea, the Button will receive clicks, and all clicks on the Rectangle will pass through to its enclosing parent:

import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick 2.7
import QtQuick.Controls 1.5
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.3


ApplicationWindow {
    width: 200; height: 150; visible: true
    property string status;

    ColumnLayout {
      Rectangle {
        width:100;height:100;

        MouseArea {
            anchors.fill: parent
            onClicked: status = "Enclosing Rectangle Clicked";    
        }

        Rectangle {
            width: 75
            height: 75
            color: "green" // it would be transparent
            opacity: 0.5
            Button {
                anchors.centerIn: parent
                height: 25; width: 25
                onClicked: status = "Button clicked";
            }
        }
      }
      Text{ text: status}
    }
}

在行动:

这篇关于QQuickView (QML) 对鼠标事件透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆