1

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

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

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

提前致谢

我的 QML 是从 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
    }
}
4

1 回答 1

0

Rectangle默认情况下对鼠标单击是透明的。如果你拿走它MouseAreaButton将会收到点击,并且所有点击都Rectangle将传递给它的封闭父:

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

在行动:

在此处输入图像描述

于 2016-11-30T00:07:21.860 回答