Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a QLineEdit got focus?

Tags:

c++

qt

qlineedit

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?

like image 742
Narek Avatar asked Dec 28 '22 14:12

Narek


1 Answers

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 !

like image 112
Andy M Avatar answered Jan 27 '23 13:01

Andy M