我有一个按钮居中的大矩形。我希望我的矩形对鼠标事件是透明的,除了按钮,它必须是可点击的。我的意思是,我希望能够用鼠标选择矩形下的代码,就像没有显示矩形一样。
我为所有大型Rect添加了一个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
}
}
答案 0 :(得分:0)
默认情况下,Rectangle
对鼠标点击是透明的。如果您取消MouseArea
,Button
将获得点击次数,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}
}
}
行动中: