tags:

views:

268

answers:

4

I want to create a login dialog by inheriting QDialog. I put in subclass named LoginDialog 2 QLineEdits:

  1. for login
  2. for password.

I want to be able to warn the user with a message if the caps lock is ON while he will start to fill passwordLineEdit. Suppose I have a function that tells the current state of CapsLock button. So I want to do eventFiltering in LoginDialog class in order to understand that user starts to fill the password field (i.e. user just stepped into the password field)

So for that purpose I wrote the following in the LoginDialog class constructor:

m_passwordLineEdit->installEventFilter(this);

So the only thing is to do is to implement a function which can understand that user is going to fill the password. Seems is should be done with the following function(??):

bool LoginDialog::eventFilter(QObject *target, QEvent *event)
{
    if (target == m_passwordLineEdit)
    {


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

MY QUESTION IS: How to implement this function???

+2  A: 

Define a new class

class LoginPassword : public QLineEdit
{
  void eventFilter(QObject *object, QEvent *event)
  }

  bool LoginPassword:eventFilter(QObject *object, QEvent *event)
  {
    if(event->type() == QEvent::KeyPress)
    {
        // Insert your logic here
    }
  }
}

In eventfilter function check for Keypressed event In this way, you will be able to catch situation when user starts typing password

Don't show popup. Its bad. Just show a tooltip type stuff like in windows xp or show a status message at bottom of login dialog

Pardeep
I can't find a simple example how to apply a tooltip(and show message) to the QLineEdit. Someone can help me?
Narek
+1  A: 

Popping up a message when the user starts to type is not a good usability style; Instead I would do it like it is done in the Windows 7 login screen and just display a warning icon and a message next to the password edit field in case the CAPSLOCK key is pressed. Keep in mind you should also check the initial state of the CAPSLOCK key.

If you're using Win32 you can use the following code to determine if the CAPSLOCK key is pressed: (::GetKeyState(VK_CAPITAL) & 0x1)

humbagumba
+1  A: 

Hello,

you could use this to get the state of the keyboard modifiers (from what the docs said I haven't tested it) but it should do the trick. http://doc.qt.nokia.com/4.6/qapplication.html#keyboardModifiers its a static function so you could call it when the text in the line edit changed like this :

connect(lineEdit , SIGNAL(textChanged()), this, SLOT(checkCapsLock));

and in checkCapsLock see if its pressed I don't know if its the best way but it should work

Olorin
In that documentation i can't see CapsLock as a keyboard modifier
Harald Scheirich
a post in the qt forum with platform depended example on how to do this http://www.qtforum.org/article/32572/how-to-determine-if-capslock-is-on-crossplatform.html I guess there is no built in way in qt to get the initial state ( or I haven't found it yet :D ) you can get in windows with GetKeyState(VK_CAPITAL)
Olorin
yes sorry I haven't noticed that capslock is not present as a keyboard modifier weird that but see my previous comment for a solution
Olorin
A: 
bool LoginDialog::eventFilter(QObject *target, QEvent *event)
{
if (target == m_passwordLineEdit)
{
    if (event->type() == QEvent::KeyPress)
    {
        if(checkCapsLock())
        {
            QToolTip::showText(m_passwordLineEdit->mapToGlobal(QPoint()), "Warning: CapsLock is ON.");
        }
        else
        {
            QToolTip::showText(m_passwordLineEdit->mapToGlobal(QPoint()), "");
        }
    }
}
return QDialog::eventFilter(target, event);
}
Narek