2014-11-03 79 views
0

我用下面的代碼設置一個按鈕按QML按鈕,但沒有任何反應

Button { 
    x: 141 
    y: 312 
    width: 98 
    height: 22 
    text: qsTr("Hello World") 
    anchors.verticalCenterOffset: 116 
    anchors.horizontalCenterOffset: -59 
    anchors.centerIn: parent 

    MouseArea 
      { 
       anchors.rightMargin: 126 
       anchors.bottomMargin: -172 
       anchors.leftMargin: -126 
       anchors.topMargin: 172 
       preventStealing: true 
       anchors.fill: parent 
       onPressed: { 
        console.debug("clicked!") 

       } 


      } 
} 

按下按鈕「世界你好」,應該點擊控制檯顯示器。

但它看起來像什麼也沒發生,當我點擊按鈕的所有QMLButton

enter image description here

您的評論歡迎

+0

QML按鈕有它自己的信號點擊,你不需要MouseArea,使用點擊信號 – Chernobyl 2014-11-03 05:28:40

+0

你是對的,謝謝 – arachide 2014-11-03 05:31:09

+0

好吧,在這種情況下,我發佈這個答案 – Chernobyl 2014-11-03 05:34:16

回答

2

首先有它自己的信號clicked,所以你不需要MouseArea

其次,如果你想使用MouseArea,你也可以嘗試onClicked信號,並做到這一點是這樣的:

Button { 
    id: button1 
    x: 8 
    y: 19 
    text: qsTr("Button") 
    //  onClicked: { 
    //   console.debug("clicked!") 
    //  } 
    MouseArea{ 
     preventStealing: true 
     anchors.fill: parent 
     onPressed: { 
      console.debug("clicked!") 
     } 
     onDoubleClicked: 
     { 
      console.debug("double clicked!") 
     } 
    } 
} 

http://qt-project.org/doc/qt-5/qml-qtquick-controls-button.html#clicked-signal

http://qt-project.org/doc/qt-5/qml-qtquick-mousearea.html#clicked-signal