问题
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