检测QML窗口外的点击次数

时间:2014-06-18 13:31:55

标签: qml qt-quick

如何在QML中检测Window {}外的点击次数?

Rectangle {
   id: topLevel

   height: 400; width: 400

   Window {
      id: windowObj

      color: "blue"
      height: 200; width: 200
      onActiveChanged { console.trace(); visible = false; }
   }

   Component.onCompleted: windowObj.visible = true
}

假设我点击了topLevel以外windowObj的部分内容。

onActiveChanged适用于Windows但不适用于MAC。

{Using: QtQuick 2.1, QtQuick.Window 2.1, QML/Qt 5.2.0}

1 个答案:

答案 0 :(得分:0)

将MouseArea放在topLevel矩形中,让事件通过

Rectangle {
   id: topLevel

   MouseArea{
      anchors.fill : topLevel
      propagateComposedEvents : true
      onClicked : console.log("clickoutside");
   }

   Window {
      id: windowObj

      color: "blue"
      height: 200; width: 200
      onActiveChanged { console.trace(); visible = false; }
   }
}