首页 > 解决方案 > 使用键盘快捷键切换可见性

问题描述

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

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

在此处输入图像描述

我的内容.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>")
    }

}

main.qml

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

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    MyContent {
        anchors.centerIn: parent
    }
}

标签: qtqml

解决方案


您必须使用该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
    }
}
// ...

推荐阅读