qtsignals-slots

Is there any way to send signal after focus on timeout in input field


I have got a similar situation, when I use 'autofocus' on any input field then with the below signal I'm able to bring up the keyboard :

connect(view, SIGNAL(loadFinished(bool)), SLOT(popupKeyboardOnAutoFocus(bool)));

where popupKeyboardOnAutoFocus is a function to bring up the keyboard.

Now, I'm trying to bring up the keyboard when focus is given to input field after a timeout of 30 sec, i.e., I have a button, after clicking which system waits for 30 seconds and then it gives focus to the input field.

Reference :

<html>
<body>

<p>Click the button to wait 3 seconds, then alert "Hello".</p>

<input type="text" id="myText" value="A text field">
<br><br><br>
<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    setTimeout(function(){ 
    document.getElementById("myText").focus();
    }, 3000);
}
</script>

</body>
</html>

Now when I try to use signal as below :

 connect(view, SIGNAL(focus(bool)),SLOT(popupKeyboardOnAutoFocus(bool)));

I'm unable to bring up the keyboard, but I can see the focus on the input field after 30sec. Where am I going wrong?


Solution

  • You need to modify your logic a bit and use this global signal :

    void QApplication::focusChanged(QWidget *old, QWidget *new)
    

    Where old is the widget that lost focut and new is the widget that just has focus.

    So you need for example to allow your popupKeyboardOnAutoFocus to comply with that signal arguments by modofying its signature, for example, to: popupKeyboardOnAutoFocus(QWidget*, QWidget*, bool)

    then you can use new signal slot syntax as folows:

    connect(app, &QApplication::focusChanged , this, &Myclass::popupKeyboardOnAutoFocus);
    

    Alternatively, If you want to keep it simple , connect your loadfinished() to a new local slot with a singleshot QTimer of 30 seconds that then calls popupKeyboardOnAutoFocus: something like:

    connect(view, SIGNAL(loadFinished(bool)), SLOT(delayElmnt()));
    

    and the delayElmnt() can be like this:

    {
    QTimer::singleShot(30000, this, SLOT(popupKeyboardOnAutoFocus()));
    }