2017-02-28 46 views
1

內部MouseArea首先獲取鼠標事件。我想「看到」這些事件,以便設置各種屬性,但不會影響它們。我想將鼠標事件傳播給任何父母MouseAreaqt qml。 MouseArea可以看到事件,但是將它們全部傳遞給父項而不影響它們?

考慮這個代碼。我想點擊藍色方塊看到「藍色按下」和「藍色釋放」,並傳遞給「父母按下」和「父母釋放」。

如果我接受該事件,父母不會得到它。如果我不接受按下,那麼我不會看到發佈。

import QtQuick 2.7 
import QtQuick.Controls 1.4 

ApplicationWindow 
{ 
    visible: true 
    width: 800 
    height: 1024 

    Rectangle 
    { 
     anchors.fill: parent 
     color: "yellow" 

     MouseArea 
     { 
      // i want these to happen even when mouse events are in the 
      // blue square 
      anchors.fill: parent 
      onPressed: console.log("parent pressed"); 
      onReleased: console.log("parent released"); 
     } 

     Rectangle 
     { 
      x: 100 
      y: 100 
      width: 100 
      height: 100 
      color: "blue" 

      // i would like to "see" events, but not affect them 
      // i want all mouse events to pass to parent, as if i am not here. 
      // however, not accepting "pressed" means i don't see "released" 
      MouseArea 
      { 
       anchors.fill: parent 
       onPressed: 
       { 
        console.log("blue pressed"); 
        mouse.accepted = false 
       } 
       onReleased: 
       { 
        console.log("blue released"); 
        mouse.accepted = false 
       } 
      } 
     } 
    } 
} 

想法歡迎。謝謝,

+0

根據文檔,設置'mouse.accepted'沒有效果。你可以改爲使用'onCancelled',如[這裏](http://doc.qt.io/qt-5/qml-qtquick-mousearea.html#canceled-signal) – DuKes0mE

+0

中所描述的那樣,也可以複製到:http:/ /stackoverflow.com/questions/23911433/qml-cant-get-mouse-released-event-when-i-dont-accept-mouse-pressed-event – DuKes0mE

+0

'onCancelled'無助於追蹤'onReleased'時另一個' MouseArea'聲明信號。另一個答案可能是對的,但可能會更清楚:*「帶有'MouseArea'這是不可能的」*。其餘的*在他看來純粹是基於觀點的,甚至沒有試圖解決問題*。相反,他的解決方案包括靜態UI,其中所有職位都是預先確定的,或者您需要跟蹤職位。至少MouseArea1需要了解MouseArea2的用途和狀態。我不認爲這是可取的。 – derM

回答

0

如果propagateComposedEvents設置爲true,則合成事件將自動傳播到場景中相同位置的其他MouseAreas。每個事件都會沿着堆疊順序傳播到下一個啓用的MouseArea,沿着這個可視層次向下傳播,直到MouseArea接受事件。一旦事件沿着層次結構前進,那麼無法在層次結構中出現,直到發生另一個鼠標事件。因此,當您在藍色矩形中設置mouse.accepted = false時,鼠標事件將轉至黃色矩形,並且它會同時接收pressedreleased信號,但上方的矩形將不再接收任何事件,直至發生另一個鼠標事件。所以,答案是否定的。

如果要處理不同的水平,如,如果你想一個MouseArea處理clicked信號和其他處理pressAndHold,或者如果你想一個MouseArea處理clicked的大部分時間,但通過它通過鼠標事件當滿足某些條件時,下面的例子可以幫助

import QtQuick 2.0 

Rectangle { 
    color: "yellow" 
    width: 100; height: 100 

    MouseArea { 
     anchors.fill: parent 
     onClicked: console.log("clicked yellow") 
    } 

    Rectangle { 
     color: "blue" 
     width: 50; height: 50 

     MouseArea { 
      anchors.fill: parent 
      propagateComposedEvents: true 
      onClicked: { 
       console.log("clicked blue") 
       mouse.accepted = false 
      } 
     } 
    } 
} 
+0

同意。答案是「不,不能做」。銘記這一點,有一個'propagatgeAllEvents'或者類似的東西會很好。 +1的建議關於其他傳播事件,所以接受這一點。 –

相關問題