tags:

views:

288

answers:

3

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 I should do that?

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

EDIT: Have wroete 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?

+1  A: 

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.

ereOn
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. :(
Narek
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, ...
Job
I would suggest showing some permanent message (eg below your line edit) when Caps Lock is on, even when it doesn't have focus.
Job
@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.
ereOn
+1  A: 

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.

Job
If he wants to know if the click focused the widget, doing so is unsafe.
ereOn
Well, unsafe is a big word:) but I edited my post to make it clear.
Job
@Job: "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.
ereOn
+2  A: 

Hey,

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 !

Andy M
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 +1
Job
This is an overkill. Should be a better approach I think. Any way thanks.
Narek