qt - SpinBox 中的 KeyNavigation 在 qml 中不起作用
问题描述
我在我的代码KeyNavigation.tab
属性中使用以使导航在 qml 中可用。
但是控件SpinBox
无法使用它。例如,如果我在它和我希望它导航的元素之间有一个控件,它不会遵守规则。
我将用一个真实的例子来说明。
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
和
main.qml
import QtQuick 2.9
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
ApplicationWindow {
id: window
title: "Stack"
visible: true
height: 200
width: 400
Item {
id: page
anchors.fill: parent
width:parent.width
height: parent.height
ScrollView {
id:scrollView
anchors.fill:parent
Column{
width:parent.width
spacing:10
TextField {
id:textField
KeyNavigation.tab: spinBox1
implicitHeight: 30
font.bold: true
}
SpinBox {
id: spinBox1
KeyNavigation.tab: spinBox2
width: 100
height: 30
editable: true
}
ComboBox {
id:comboBox
//KeyNavigation.tab: spinBox2
anchors.topMargin: 10
textRole: "text"
}
SpinBox {
id: spinBox2
KeyNavigation.tab: textField
width: 100
height: 30
editable: true
}
}
}
}
}
如果我们使用制表符,这里spinBox1
不会跳转。spinBox2
这是在 Windows 10 操作系统上测试的
使用的 Qt 版本是 5.11.1
解决方案
出于某种原因,如果附加属性设置在 SpinBox 本身而不是其 TextInput 上,则不会调用 QQuickKeyNavigationAttached::keyPressed()。因此,使用 contentItem 上的附加属性是一种解决方法:
SpinBox {
Component.onCompleted: contentItem.KeyNavigation.tab = spinBox
}
推荐阅读
- sql - 递归 CTE - 根据子值计算父值
- java - 为什么将 ResultSetType 从 TYPE_SCROLL_INSENSITIVE 更改为 TYPE_FORWARD_ONLY 会更改 SQL 查询的结果?
- python - 使用 matplotlib 将数组转换为图像
- scala - Spark 无法捕获 Size超过 Integer.MAX_VALUE 错误异常?
- c# - 删除开始和结束之间的字符串的一部分
- android - 如何在我的用户名字符串旁边添加一个自动增量整数?
- amazon-web-services - dynamodb 中的哈希键和范围键是什么以及如何使用它们
- indexing - 未定义索引:nid
- sql-server - TSQL 运行总计
- indexing - 由于被 robots.tx 阻止,Mailchimp 登录页面无法在 Google Search Console 上编入索引