Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML focus specific window.



  • Hey,

    I'm currently trying to focus a specific window from multiple windows.
    I've tried to do it with the requestActivate, or active flag from the qml Window http://doc.qt.io/qt-5/qml-qtquick-window-window.html
    but it didn't work.

    Additionally I'm working with the Qt window modality, so the focus should be on the top window.



  • @Slei
    How do you create your multiple QML windows?



  • @Diracsbracket

    As childs directly in qml

    MainUI
    ---------> Settings UI
    -----------------------> Addintional UI



  • @Slei
    My question implied which method you were using to create the additional windows.



  • @Diracsbracket

    They are instantiated in qml as childs and therefore created when the mainui is created.
    and i display them by using show()



  • @Slei
    You mean something like:

    ApplicationWindow {
        id: mainWindow
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Row {
            Button {
                text: "show window 1"
                onClicked: {
                    if (!window1.visible)
                        window1.show()
    
                    window1.requestActivate()
                }
            }
            Button {
                text: "show window 2"
                onClicked: {
                    if (!window2.visible)
                        window2.show()
    
                    window2.requestActivate()
                }
            }
        }
    
        Window {
            id: window1
            height: 300
            width: 600
    
            Rectangle {
                anchors.fill: parent
                color: "red"
            }
            TextField {
                id: textField1
                focus: true
                placeholderText: "text 1"
            }
        }
    
        Window {
            id: window2
            height: 300
            width: 600
    
            Rectangle {
                anchors.fill: parent
                color: "blue"
            }
    
            TextField {
                focus: true
                id: textField2
                placeholderText: "text 2"
            }
        }
    }
    

    If so, it seems to work correctly? If you want to get the parent window on top, that is not possible here.



  • @Diracsbracket
    yeah something like this, but the Windows {} stuff is in a seperate qml and i just instatiate it in the main ui qml
    I actually don't want the parent on top, it good taht the child is on top, but i currently need to find a way to focus the top window without using show().
    The problem is when i set the ui to visible=false and back to true nothing is focused, and when i use requestActivate only the main ui ( parent) gets focused even though i use modality, so the user can't use the parent while the child is open.



  • @Slei said in QML focus specific window.:

    i currently need to find a way to focus the top window without using show().

    The problem is when i set the ui to visible=false and back to true nothing is focused

    Wouldn't a focus or forceActiveFocus in the onVisibleChanged() handler of each of your windows help? When the Window is created and not hidden and no other window created after that, does the intended element in the window correctly get the focus?



  • @Diracsbracket said in QML focus specific window.:

    forceActiveFocus

    when the windows gets visible=true after being visible=false it gets focused correctly
    the main issue currently is, when the window is visible but not focused, for example the user uses the windows explorer window and therefore my ui isn't focused anymore.

    But i provide a systemtray icon "Show UI" which brings the window back to the top with the qml function "raise()", but it doesn't focus my ui.

    I've tried the following command:

    		raise();
    		forceActiveFocus();
    		requestActivate();
    

    and none of those work for focusing my ui when bringing it back to the top when being visible.



  • @Slei
    On my system (Win10 Pro, Qt 11.0, VS2017 Community) this works without any problem: the system tray icon does its job correctly?

    Could you try with this minimal example below?

    Press the buttons 1 and 2 to show the windows. Then, in window1, you can start editing textField2, then switch to something else (thus textField2 is still in edit mode), e.g. Windows Explorer, or whatever.

    Then press the icon in the tray. It should bring Windows1 up and (forcibly) remove focus from textField2 (even though it is still in editing mode, by issuing its accepted() signal, just for illustration) and put focus on textField1.

    import QtQuick 2.11
    import QtQuick.Window 2.11
    import QtQuick.Controls 2.4
    import Qt.labs.platform 1.0
    
    Window {
        id: appWindow
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        SystemTrayIcon {
            visible: true
            iconSource: "qrc:/images/tray-icon.png"
    
            onActivated: {
                window1.show() //not needed if already showing
                window1.raise()
                window1.requestActivate()
                if (window1.textField12.focus === true)
                    window1.textField12.accepted()
                window1.textField1.forceActiveFocus()
            }
        }
    
        Row {
            Button {
                text: "show window 1"
                onClicked: {
                    if (!window1.visible)
                        window1.show()
    
                    window1.requestActivate()
                }
            }
            Button {
                text: "show window 2"
                onClicked: {
                    if (!window2.visible)
                        window2.show()
    
                    window2.requestActivate()
                }
            }
        }
    
        Window {
            id: window1
            height: 300
            width: 600
    
            property alias textField12: textField12
            property alias textField1: textField1
    
            Rectangle {
                anchors.fill: parent
                color: "red"
            }
            Column {
                TextField {
                    id: textField1
                    focus: true
                    placeholderText: "text 1"
                }
    
                TextField {
                    id: textField12
                    placeholderText: "text 2"
                }
            }
        }
    
        Window {
            id: window2
            height: 300
            width: 600
    
            Rectangle {
                anchors.fill: parent
                color: "blue"
            }
    
            TextField {
                focus: true
                id: textField2
                placeholderText: "text 2"
            }
        }
    }
    


  • @Diracsbracket

    your example works, the difference is that you have to click in to the window anyway to press the button, which focuses the window.
    in my case the window is in the back somewhere and i try to activate it through the systemtray icon, therefore im not directly activating any window of the app through clicks

    Clicking the appicon in the task bar, works correctly for example, it has automatically the focus on the top window. So there must be somehow an issue with the activating/showing the window through my systemtray icon.

    Currently I do the following:

    Click on Systemtray Settings UI -> emit qmlShow() of Settings UI

    So i don't activate anything direclty with the main ui or application, I only call qmlShow for the settings ui:

    SettingsUI.qml
    onQmlShow:

    	       visible = true;
    		raise();
    		requestActivate();
    		show();
    		active = true;
    		focus = true;
    

    Thats it.

    This doesn't work because after using the function the window is in the front but the foucs is still somewhere on the desktop or windows taskbar.

    Maybe I just miss a step for this to work correctly on windows, like activating the whole main ui first and then calling request activate, I'll test those and see if it somehow works



  • @Slei
    My second example works from the system tray as well. The buttons are only there to initially show the windows. Then, even if window 2 has the foreground, clicking on the system tray will put window1 in the foreground, and give focus to the textfield immediately so you can start typing immediately without clicking the text field.



  • @Diracsbracket oh i havent seen the second example lol, anyway i think i have found the issue.

    I've tried to call qmlShow first on the main window which simply sets the visiblity to true and also calls raise()

    and after this i call the qmlShow() for the child window which is in the front which executes the code posted previously.

    So it seems like I need to first activate/raise the main ui and afterwards the sub ui since this works perfectly.



  • just notice the main issue with this is, that the main ui needs to be visible aswell and forces the main ui to show, so I use show for the settings ui without showing the mainui.
    but this is probably an general issue with instancing the settings ui in the main ui


Log in to reply