qt - 使用键盘快捷键切换可见性
问题描述
如何使用键盘快捷键切换搜索栏的可见性?
默认情况下,我希望隐藏搜索栏。但是,然后用户按 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
}
}
解决方案
您必须使用该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
}
}
// ...
推荐阅读
- python - DataFrame 切片和列表的交集
- javascript - 在模态对话框中创建按钮并在电子表格中运行代码
- flutter - 如何使用flutter_blue在flutter BLE中获取原始制造商数据
- php - cURL在sql中获取json数据存储
- java - Causedby:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter”
- javascript - ReferenceError:从另一个模块导入时未定义变量
- javascript - 重构我臃肿的代码的好方法是什么?
- autodesk-forge - 使用 BIM 360 Docs API 有没有办法使用现有项目设置创建项目?
- firebase - 从字符串格式的“gs”url创建firebase存储引用
- node.js - 如何在 node.js 中为单独的路由文件和 api 文件更正我的代码