Qt里对QLineEdit输入框进行判断

主要重写 bool eventFilter(QObject *,QEvent *);这个函数,在代码中的每个QlineEdit下加上connect(m_labelLinkRegister,SIGNAL(linkActivated(QString)),this,SLOT(LinkOpen(QString)));其中,m_labelLinkRegister为QLabel的指针对象。

bool LHGoldenfarmClientLogin::eventFilter(QObject *watched, QEvent *event)
{
    QString strUser = m_textUser->text();
    QString strPassword = m_textPassword->text();


    if(watched == this->m_textUser)
    {
        if(event->type() == QEvent::FocusIn)
        {
            if(m_strUser == "")
            {
                this->m_textUser->setText("");
            }
        }
        else if(event->type() == QEvent::FocusOut)
        {
            if(strUser == "")
            {
                this->m_textUser->setText("Username");
            }
            else
                m_strUser = strUser;
        }
    }
    if(watched == this->m_textPassword)
    {
        if(event->type() == QEvent::FocusIn)
        {
            if(m_strPassword == "")
            {
                this->m_textPassword->setText("");
            }
        }
        else if(event->type() == QEvent::FocusOut)
        {
            if(strPassword == "")
            {
                this->m_textPassword->setText("Username");
            }
            else
                m_strPassword = strPassword;
        }
    }
    return QDialog::eventFilter(watched,event);
}
这是要重写的函数,现在我们就可以根据鼠标的焦点进行判断,并设置输入框的内容了。

你可能感兴趣的:(网络)