How to know if a QLineEdit got focus?
Asked Answered
C

3

4

I want to be able to know if in the QLineEdit it was a click. So I guess I should reimplement the following function(??):

void QLineEdit::focusInEvent ( QFocusEvent * e )   [virtual protected]

How should I do that?

Also, please tell me how to use focusInEvent() function in order to know if QLineEdit myEdit; object got focus.

EDIT: I have written the following function:

bool LoginDialog::eventFilter(QObject *target, QEvent *event)
{
    if (target == m_passwordLineEdit)
    {
        if (event->type() == QEvent::FocusIn)
        {
            if(checkCapsLock())
            {
                QMessageBox::about(this,"Caps Lock", "Your caps lock is ON!");

            }
            return true;

        }
    }
    return QDialog::eventFilter(target, event);
}

And have registered m_passwordLineEdit in LoginDialog class constructor like this:

m_passwordLineEdit->installEventFilter(this);

And it's falling into an infinite loop of MessageBox-es. Please help me to resolve this situation. Actually I would like to implemet this function with a pop-up window (not with a QMessageBox). Is it OK to use QLabel for that need?

Chink answered 10/6, 2010 at 7:13 Comment(0)
W
4

Something like that:

class YourWidget : public QLineEdit
{
    Q_OBJECT

    protected:

    void focusInEvent(QFocusEvent* e);
};

In the .cpp file:

void YourWidget::focusInEvent(QFocusEvent* e)
{
    if (e->reason() == Qt::MouseFocusReason)
    {
      // The mouse trigerred the event
    }

    // You might also call the parent method.
    QLineEdit::focusInEvent(e);
}

You can find the list of all possible reasons on this page.

Wira answered 10/6, 2010 at 7:23 Comment(4)
I wanted to implement with bool eventFilter(QObject *target, QEvent *event) in order to not write a Subclass. Have written the following code: bool LoginDialog::eventFilter(QObject *target, QEvent *event) { if (target == m_passwordLineEdit) { if (event->type() == QEvent::FocusIn) { if(checkCapsLock()) { QMessageBox::about(this,"Caps Lock", "Your caps lock is ON!"); } return true; } } return QDialog::eventFilter(target, event); } and I am falling into infinite loop of MessageBox-es. :(Chink
That's because your m_passwordLineEdit will lose focus when a message box is shown and get focus again when this box is closed, resulting in showing the box again, ...Instrumental
I would suggest showing some permanent message (eg below your line edit) when Caps Lock is on, even when it doesn't have focus.Instrumental
@Narek: Job gave the correct explanation of the behavior you're confronted to. Add a permanent message (in a red QLabel) and display it or hide it depending on the status of the caps lock key. Beware that pressing caps lock while your QLineEdit is still focused won't trigger focusInEvent(). You'll have to handle that case too.Wira
N
6

Also, please tell me how to use focusInEvent() function in order to know if QLineEdit myEdit; object got focus.

You should connect yourself to the following SIGNAL :

void QApplication::focusChanged ( QWidget * old, QWidget * now )   [signal]

When the new QWidget is your QLineEdit, you know it got focus !

Hope it helps !

Nessie answered 10/6, 2010 at 9:14 Comment(3)
Ah didn't know that signal. This is very useful because you don't have to inherit a widget just to override focusInEvent! The downside is that you have to react on every focus change in your application. Still worth a +1Instrumental
This is an overkill. Should be a better approach I think. Any way thanks.Chink
how would that be done in Python ? I do not want to build a custom class for Filters and then install that as QLineEdit.installEventFilter(CustomeFilterClass)Cinquain
W
4

Something like that:

class YourWidget : public QLineEdit
{
    Q_OBJECT

    protected:

    void focusInEvent(QFocusEvent* e);
};

In the .cpp file:

void YourWidget::focusInEvent(QFocusEvent* e)
{
    if (e->reason() == Qt::MouseFocusReason)
    {
      // The mouse trigerred the event
    }

    // You might also call the parent method.
    QLineEdit::focusInEvent(e);
}

You can find the list of all possible reasons on this page.

Wira answered 10/6, 2010 at 7:23 Comment(4)
I wanted to implement with bool eventFilter(QObject *target, QEvent *event) in order to not write a Subclass. Have written the following code: bool LoginDialog::eventFilter(QObject *target, QEvent *event) { if (target == m_passwordLineEdit) { if (event->type() == QEvent::FocusIn) { if(checkCapsLock()) { QMessageBox::about(this,"Caps Lock", "Your caps lock is ON!"); } return true; } } return QDialog::eventFilter(target, event); } and I am falling into infinite loop of MessageBox-es. :(Chink
That's because your m_passwordLineEdit will lose focus when a message box is shown and get focus again when this box is closed, resulting in showing the box again, ...Instrumental
I would suggest showing some permanent message (eg below your line edit) when Caps Lock is on, even when it doesn't have focus.Instrumental
@Narek: Job gave the correct explanation of the behavior you're confronted to. Add a permanent message (in a red QLabel) and display it or hide it depending on the status of the caps lock key. Beware that pressing caps lock while your QLineEdit is still focused won't trigger focusInEvent(). You'll have to handle that case too.Wira
I
0

If you want to know when someone clicks in a widget, you should override mousePressEvent (QMouseEvent* event). A focusInEvent can be triggered by other sources than a mouse click.

For example:

class MyLineEdit : public QLineEdit
{
        Q_OBJECT
    public:
        //...
    protected:
         void mousePressEvent(QMouseEvent* event)
         {
              //pass the event to QLineEdit
              QLineEdit::mousePressEvent(event);
              //register the click or act on it
         }
};

If you do want to know when your widget receives focus, do the same with a focusInEvent of course.

Instrumental answered 10/6, 2010 at 7:25 Comment(2)
If he wants to know if the click focused the widget, doing so is unsafe.Wira
"unsafe" might indeed be too much ;) I meant that some widget might trigger a mousePressEvent() but still not get the focus. But you got my point.Wira

© 2022 - 2024 — McMap. All rights reserved.