Dialog forceActiveFocus of first Item in tab key nav loop
-
I can create a qml item that will give active focus to the first item in a tab key nav loop. This works.
If I put the same qml item in a Dialog it does not give active focus to the first item in the loop.
Where the whole point of a Dialog is generally for user input this seems like this should be easier.main.qml:
import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Dialogs 1.3 import QtQuick.Controls 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Tab Key Navigation in a Dialog") Button { id: makedialog text: "dialog" onClicked: { dialogloader.sourceComponent = tabcyclecomp } } // this works // focus is gained by the first item in the repeater TabCycleComp { y: makedialog.height + 10 } // this does not // tab nav works, but focus is not gained on the first item Loader { id: dialogloader anchors.centerIn: parent width: 400 height: 400 } Component { id: tabcyclecomp Dialog { visible: true width: 400 height: 400 standardButtons: StandardButton.Ok | StandardButton.Cancel TabCycleComp { anchors.centerIn: parent width: parent.width } onClosed: { dialogloader.sourceComponent = null } } } }
TabCycleComp:
import QtQuick 2.15 import QtQuick.Window 2.15 Column { id: column spacing: 5 Repeater { id: repeater model: 10 Rectangle { height: 20 width: 100 color: "transparent" border.width: 1 border.color: activeFocus ? "steelblue" : "black" property alias textInput: textinput TextInput { id: textinput anchors.fill: parent anchors.margins: 2 KeyNavigation.tab: { var item if((index+1) >= repeater.count){ item = repeater.itemAt(0) }else{ item = repeater.itemAt(index+1) } return item.textInput } Component.onCompleted: { if(index === 0){ forceActiveFocus() } } } } } }
main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
-
@fcarney said in Dialog forceActiveFocus of first Item in tab key nav loop:
> Dialog { > visible: true > focus: true > width: 400 > height: 400 > > standardButtons: StandardButton.Ok | StandardButton.Cancel > > TabCycleComp { > anchors.centerIn: parent > width: parent.width > } > > onClosed: { > dialogloader.sourceComponent = null > } > }
All I had to do was change one thing! Added focus: true to Dialog! I swear I tried this... Maybe not.