最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

qt - How to propagate mouse event to TextField from MouseArea - Stack Overflow

programmeradmin4浏览0评论

I have some little app

Window {
    width: 640; height: 480; visible: true; title: qsTr("Hello")

    Rectangle {
        color: "green"; width: 100; height: 20
        anchors.centerIn: parent

        TextField {
            anchors.fill: parent; anchors.margins: 2
        }

        MouseArea {
            anchors.fill: parent; propagateComposedEvents: true
            hoverEnabled: true
            onClicked: {
                mouse.accepted = false
            }
        }
    }
}

propagateComposedEvents set true When I clicked, cursor does not set to TextField. But if I click middle button all is ok. How can I propagate mouse click to TextField?

I have some little app

Window {
    width: 640; height: 480; visible: true; title: qsTr("Hello")

    Rectangle {
        color: "green"; width: 100; height: 20
        anchors.centerIn: parent

        TextField {
            anchors.fill: parent; anchors.margins: 2
        }

        MouseArea {
            anchors.fill: parent; propagateComposedEvents: true
            hoverEnabled: true
            onClicked: {
                mouse.accepted = false
            }
        }
    }
}

propagateComposedEvents set true When I clicked, cursor does not set to TextField. But if I click middle button all is ok. How can I propagate mouse click to TextField?

Share Improve this question asked Mar 17 at 15:33 Евгений ДружининЕвгений Дружинин 3912 gold badges5 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

your Problem is that you are handling the wrong event in the mouse area. TextField gets focus on mouse pressed(!) not on mouse clicked. Therefor the correct solution is the following:

Window {
    width: 640; height: 480; visible: true; title: qsTr("Hello")

    Rectangle {
        color: "green"; width: 100; height: 20
        anchors.centerIn: parent

        TextField {
            anchors.fill: parent; anchors.margins: 2
        }

        MouseArea {
            anchors.fill: parent
            propagateComposedEvents: true
            hoverEnabled: true

            onClicked: event => {
                // error: textfield gets focus on press not on clicked!
                event.accepted = false
                console.log("clicky")
            }
            onPressed: event => {
                // correct: set the press event to accepted false to propergate it to the textfield!
                event.accepted = false
                console.log("pressed event accepted false")
            }
        }
    }
}

No need for MouseArea. You can set selectByMouse to true. You can also set hoverEnabled on TextField.

TextField {
    id: textField
    anchors.centerIn: parent
    background: Rectangle {
        border.color: textField.hovered || textField.activeFocus ? "green" : "lightgrey"
        border.width: 1
    }
    implicitWidth: 100
    hoverEnabled: true
    selectByMouse: true
}

You can Try it Online!

发布评论

评论列表(0)

  1. 暂无评论