Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Dialog forceActiveFocus of first Item in tab key nav loop
Forum Updated to NodeBB v4.3 + New Features

Dialog forceActiveFocus of first Item in tab key nav loop

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 468 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    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();
    }
    

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #2

      @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.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved