nextItemInFocusChain function not working in qml on mac?

家住魔仙堡 提交于 2021-02-11 12:07:06

问题


I have a form where i've done a custom field that only accepts the top, right and bottom arrows.

But to accept the tab navigation i'm using the following chain of functions:

nextItemInFocusChain().forceActiveFocus()

The problem is that this is working on windows but not on mac...

I've a formulary to illustrate the problem where next to that "code text field" i have a comboBox where i want, that when the user clicks on tab in the "code text field" to navigate to.

It seems that it only navigates to other textFields, and a spinBox, like i have on the example seems to have a textField as a contentItem.

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 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
            Column{
                width:parent.width
                spacing:10
                TextField {
                    id:textField
                    KeyNavigation.tab: spinBox1
                    implicitHeight: 30
                    font.bold: true
                }
                SpinBox {
                    id: spinBox1
                    width: 100
                    height: 30
                    editable: true
                    Component.onCompleted: contentItem.KeyNavigation.tab = userCodeField
                }
                PanelCodeField {
                    id: userCodeField
                    width: 100
                    height: 30
                    KeyNavigation.tab: comboBox
                }
                ComboBox {
                    id:comboBox
                    anchors.topMargin: 10
                    model: [ "Banana", "Apple", "Coconut" ]
                    KeyNavigation.tab: spinBox2
                }
                SpinBox {
                    id: spinBox2
                    width: 100
                    height: 30
                    editable: true
                    Component.onCompleted: contentItem.KeyNavigation.tab = textField
                }
            }
    }
}

PanelCodeField.qml

import QtQuick 2.0

PanelTextField {
    height: 479.9
    visible: true
    maximumLength: 5
    font.pointSize: 12
    property bool jumpOnTab: false
    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
        case Qt.Key_Tab:
            if(jumpOnTab)
                nextItemInFocusChain().nextItemInFocusChain().forceActiveFocus()
            else
                nextItemInFocusChain().forceActiveFocus()
            event.accepted = true
            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
        }
    }
}

PanelTextField.qml

import QtQuick 2.0
import QtQuick.Controls 2.0

TextField {
    property var linkedData
    implicitHeight: 30
    font.bold: true
    implicitWidth:parent.width
}

Am i doing something wrong for the mac os x, or is there a workaround?


回答1:


Open System Preferences > Keyboard > Shortcuts and select All Controls. By default macOS only allows tab navigation between "Text boxes and lists only".



来源:https://stackoverflow.com/questions/55060204/nextiteminfocuschain-function-not-working-in-qml-on-mac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!