现在好多界面都用到了一种提示框,上面显示的是“请输入。。。”之类的字样,当鼠标点进去的时候变成空的,等待输入,在不输入任何字符的情况下,移开鼠标,提示框中又显示提示字符,就像QQ上的搜索框那栏。

  其实这种这种效果就是必须对输入框中的鼠标移进移出做出处理。因此重写lineedit的focusInEvent和focusOutEvent就行了。

 

CLineEditFocus::CLineEditFocus()
{
 m_boolEdit = false;
 m_nfocusStatus = FOCUS_OUT;

 connect(this, SIGNAL(textEdited(QString)), this, SLOT(slotTextEdit(QString)));
}


CLineEditFocus::~CLineEditFocus()
{

}


void CLineEditFocus::setDefault(QString str)
{
 //QLineEdit::setStyleSheet("color: red;");
 setText(str);
 m_strDefault = str;
 m_boolEdit = false;
}


 
void CLineEditFocus::focusInEvent ( QFocusEvent * event )
{
 QLineEdit::focusInEvent( event );//必须加上,不然没有光标
 m_nfocusStatus = FOCUS_IN;
 if (false == m_boolEdit)
  setText("");
 //QLineEdit::setStyleSheet("color: black;");
}


void CLineEditFocus::focusOutEvent ( QFocusEvent * event )
{
 QLineEdit::focusOutEvent( event );
 m_nfocusStatus = FOCUS_OUT;
 if (text() == "")
 {
  //QLineEdit::setStyleSheet("color: red;");
  setText(m_strDefault);
  m_boolEdit = false;
 }
 else
 {
  m_boolEdit = true;
 }
}