仅通过某些键(包括tab和QML输入)起作用的TextField

时间:2018-12-06 14:34:15

标签: qt qml

我有一个TextField,我想将其限制为几个键,如果单击该键,它将在TextField上放置一个特定的值,但是我仍然希望{{1 }}和tab键起作用,如果我单击这两个键中的任何一个,则焦点应移至下一个控件。

该限制enter的代码为:

TextField

完整的示例:

                    TextField {
                    height: 479.9
                    Layout.fillWidth: true
                    Layout.rightMargin: 10
                    maximumLength: 5
                    Keys.onPressed: {
                        var c
                        switch (event.key) {
                        case Qt.Key_Up:
                            c = String.fromCharCode(0x25B2)
                            break
                        case Qt.Key_Down:
                            c = String.fromCharCode(0x25BC)
                            break
                        case Qt.Key_Right:
                            c = String.fromCharCode(0x25B6)
                            break
                        default:
                            event.accepted = true
                            break
                        }
                        if (!event.accepted) {
                            var s = text.concat(c)
                            text = s.substr(Math.max(0,s.length-maximumLength), maximumLength)
                            event.accepted = true
                        }
                    }
                    onFocusChanged: if(focus) { configScroll.scrollToY(y); }
                }

那么,为了使import QtQuick 2.9 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 ApplicationWindow { id: window title: "Stack" visible: true width: 1400 Page { id: page anchors.fill: parent property int responsiveWidth: 1000 property int maximumWidth: 900 ScrollView { anchors.fill: parent function scrollToY(y) { //TODO: Não funciona com import QtQuick.Controls 1.4 configScroll.flickableItem.contentY = y-30 } GridLayout { columns: 2 Keys.onPressed: { if(event.key==Qt.Key_Return) for (var i = 0; i < children.length; ++i) if (children[i].focus) { children[i].nextItemInFocusChain().forceActiveFocus() break } } width: page.width > page.responsiveWidth ? page.maximumWidth : page.width anchors.top: parent.top anchors.left: parent.left anchors.leftMargin: page.width > page.responsiveWidth ? (page.width - childrenRect.width)/2 : 10 anchors.rightMargin: page.width > page.responsiveWidth ? 0 : 10 Label { text: "Company Name" color: "red" Layout.fillWidth: true } TextField { objectName: "company_name" font.bold: true Layout.fillWidth: true Layout.rightMargin: 10 onFocusChanged: if(focus) { configScroll.scrollToY(y); } } Label { text: "test" color: "red" Layout.fillWidth: true } TextField { height: 479.9 Layout.fillWidth: true Layout.rightMargin: 10 maximumLength: 5 Keys.onPressed: { var c switch (event.key) { case Qt.Key_Up: c = String.fromCharCode(0x25B2) break case Qt.Key_Down: c = String.fromCharCode(0x25BC) break case Qt.Key_Right: c = String.fromCharCode(0x25B6) break default: event.accepted = true break } if (!event.accepted) { var s = text.concat(c) text = s.substr(Math.max(0,s.length-maximumLength), maximumLength) event.accepted = true } } onFocusChanged: if(focus) { configScroll.scrollToY(y); } } Label { text: "label" color: "red" Layout.fillWidth: true } TextField { objectName: "company_name" font.bold: true Layout.fillWidth: true Layout.rightMargin: 10 onFocusChanged: if(focus) { configScroll.scrollToY(y); } } } } } } TextField重新工作,我需要对tab进行哪些更改?

0 个答案:

没有答案