我遇到了一个特定的问题,当我单击SpinBox中间的文本时,它会获得焦点,但在此之后,我转到另一个SpinBox,然后单击-或+,当我这样做时,我希望将焦点从最后一个SpinBox转移到新的for,以便假定该值。
例如,假设我单击顶部的SpinBox:

现在,我单击底部SpinBox上的+按钮,这样做是为了将焦点放在这个底部SpinBox上

我提供了下面的main.qml代码:
import QtQuick 2.9
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Styles 1.4
ApplicationWindow {
id: window
title: "Stack"
visible: true
height: 200
width: 400
ListModel {
id: libraryModel
ListElement {
text: "A Masterpiece"
}
ListElement {
text: "Brilliance"
}
ListElement {
text: "Outstanding"
}
}
Item {
id: page
anchors.fill: parent
width:parent.width
height: parent.height
ScrollView {
id:scrollView
anchors.fill:parent
style: ScrollViewStyle{
handle: Rectangle {
implicitWidth: 30
color: "black"
}
scrollToClickedPosition: true
transientScrollBars:true
}
function scrollToY(y) {
scrollView.contentItem.contentY = y;
}
Column{
width:parent.width
spacing:10
TextField {
id:textField
implicitHeight: 30
font.bold: true
onFocusChanged: if(focus) { scrollView.scrollToY(y); }
}
ComboBox {
id:comboBox
anchors.topMargin: 10
textRole: "text"
model: libraryModel
onFocusChanged: if(focus) { scrollView.scrollToY(y); }
}
SpinBox {
width: 100
height: 30
editable: true
}
SpinBox {
width: 100
height: 30
editable: true
}
}
}
}
}如何做到这一点?
发布于 2019-01-28 19:43:49
您可以使用focusPolicy
import QtQuick 2.0
import QtQuick.Controls 2.0
ApplicationWindow {
id: window
height: 200
width: 400
visible: true
Column{
spacing: 10
SpinBox {
width: 100
height: 30
editable: true
}
SpinBox {
width: 100
height: 30
editable: true
focusPolicy: Qt.ClickFocus // or Qt.StrongFocus
}
}
}单击控件时,Qt.ClickFocus会将焦点放在控件上,Qt.TabFocus表示选项卡,Qt.WheelFocus表示轮子,Qt.StrongFocus表示所有三个控件。
https://stackoverflow.com/questions/54400858
复制相似问题