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

MessageDialog responsive to keyboard events.



  • Hello,

    Is there a way to have a MessageDialog responsive to keyboard events ?
    I tried to catch a return key signal (like the following snippet ) but it's not working.

    MessageDialog {
    
        id: failed_popup
        property string message : failed_popup.text
        property string errorTitle : failed_popup.title
        property alias icon : failed_popup.icon
        title: errorTitle
        icon: StandardIcon.Warning
        standardButtons: StandardButton.Ok
        text: message
    
        Keys.onReturnPressed: {
            console.log(" Return pressed ")
            accepted();
        }
    }
    

    I know I could use another QML component like window but I was expected this to be working in MessageDialog directly as I don't need anything more complicated..
    Am I missing something ?


  • Moderators

    @EmilieG that should work, are you sure you're pressing return and not enter ? that can vary from region to region and keyboard to keyboard and os to os

    also try forcing active focus on the MessageDialog

    activeFocus:true

    because maybe another component (for example the button) has active focus after displaying the Dialog



  • @J-Hilk Thanks for your answer. There is no activeFocus or focus property for the MessageDialog, so it's not compiling..


  • Moderators

    @EmilieG
    ok, I tried your example code and I actually get the following message:

    Could not attach Keys property to: DefaultMessageDialog_QMLTYPE_35_QML_37(0x7fcb04c5fe40) is not an Item

    so, you can't catch the key event 🤷‍♂️

    You could possibly install an eventFilter in main.cpp that should work


Log in to reply