QQuickView (QML) transparent for mouse events

↘锁芯ラ 提交于 2021-01-29 03:29:26

问题


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.

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

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.

Thanks in advance

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:

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
    }
}

回答1:


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}
    }
}

In action:



来源:https://stackoverflow.com/questions/40872343/qquickview-qml-transparent-for-mouse-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!