使用键盘快捷键切换可见性

时间:2019-06-12 14:29:48

标签: qt qml

如何使用键盘快捷键切换搜索栏的可见性?

默认情况下,我想隐藏搜索栏。但是,然后用户按Ctrl + F我希望它可见。然后,当他们单击“ X”按钮时,它将再次隐藏。

enter image description here

MyContent.qml

if(led.endsWith("1"))
...
else if(led.endsWith("0"))

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3


ColumnLayout {
    anchors.fill: parent

    RowLayout {
        width: parent.width
        visible: true

        TextField {
            text: ""
            placeholderText: qsTr("Search...")
            selectByMouse: true
            Layout.fillWidth: true
        }

        Button {
            text: "x"
        }
    }


    Label {
        wrapMode: Text.Wrap
        font.pixelSize: Qt.application.font.pixelSize * 1.1

        Layout.fillWidth: true
        Layout.fillHeight: true

        text: qsTr("<h1>Stuff goes here</h1><p>this is just some sample text</p>")
    }

}

1 个答案:

答案 0 :(得分:1)

您必须使用Shortcut组件:

// ...
RowLayout {
    id: row
    width: parent.width
    visible: false

    Shortcut {
        sequence: "Ctrl+F"
        onActivated: {
            row.visible = true
            tf.focus = true
        }
    }

    TextField {
        id: tf
        text: ""
        placeholderText: qsTr("Search...")
        selectByMouse: true
        Layout.fillWidth: true
    }

    Button {
        text: "x"
        onClicked: row.visible = false
    }
}
// ...