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

Force active focus on top QML Window



  • Hi,

    I swear I've done this before on a previous project, but I can't for the life of me figure out how to do this! I've got an ApplicationWindow, with a Loader which opens an external QML Window. The new Window uses the WindowStaysOnTopHint flag, so it remains the top window in the application.

    However, I'm still able to interact with the ApplicationWindow beneath (hover events, clicking, etc.), including losing focus to the ApplicationWindow beneath when clicking the mouse outside of the top Window. Is there a way to 'lock' the focus on the top Window, such that any clicks outside the Window are ignored?

    Here's a high-level representation of my code:

    main.qml:

    ApplicationWindow {
        id: mainWindow
        visible: true
        title: "Main Window"
    
        //RowLayouts, Columns, etc.
    
        Loader {
            id: loader
        }
    
        //Elsewhere in the ApplicationWindow, a MouseArea event sets the loader source to other.qml
    }
    

    other.qml:

    Window {
        id: otherWindow
        visible: true
        title: "Other Window"
        flags: Qt.WindowStaysOnTopHint
        minimumWidth: 400
        minimumHeight: 400
        color: "red"
    }
    

    As you can see, it's a fairly simple use case; I just need to ensure that once the 'Other Window' has been loaded, all focus/mouse activity outside of this window (i.e. clicking on the Main Window below) is ignored/prevented.



  • It sounds like you're describing an application-modal window.

    Window {
        Window.modality: Qt.ApplicationModal
    }
    


  • @jeremy_k That's it! Thank you very much, I knew it was something simple I'd forgotten.


Log in to reply