【问题标题】:press qml button but no response按qml按钮但没有反应
【发布时间】:2014-11-03 05:01:10
【问题描述】:

我使用下面的代码设置了一个按钮

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!")

                }


            }
}

按下按钮“hello world”,它应该显示在控制台上单击。

但是当我点击按钮时似乎什么都没有发生

欢迎评论

【问题讨论】:

  • QML Button 有自己的点击信号,所以你不需要 MouseArea,使用点击信号
  • 你是对的,谢谢
  • 好的,在这种情况下,我将其发布为答案

标签: qt qml


【解决方案1】:

首先QMLButton有自己的信号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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2023-03-21
    • 1970-01-01
    • 2017-10-02
    • 2019-06-18
    • 2019-01-14
    相关资源
    最近更新 更多