00001 #include "alineedit.h" 00002 00003 ALineEdit::ALineEdit(const QString &defaultValue, QWidget *parent) : QLineEdit(parent) 00004 { 00005 defaultText = defaultValue; 00006 if(this->hasFocus()) 00007 { 00008 hideHintText(); 00009 } 00010 else 00011 { 00012 showHintText(); 00013 } 00014 } 00015 00016 ALineEdit::ALineEdit(QWidget *parent) : QLineEdit(parent) 00017 { 00018 } 00019 00020 QString ALineEdit::hintText() const 00021 { 00022 return defaultText; 00023 } 00024 00025 void ALineEdit::setHintText(const QString &hintText) 00026 { 00027 // Updating magic to make the property work in Designer. 00028 if(hintText == this->text()) 00029 { 00030 return; 00031 } 00032 if(this->text() == defaultText) 00033 { 00034 defaultText = hintText; 00035 this->setText(QString()); 00036 } 00037 else 00038 { 00039 defaultText = hintText; 00040 } 00041 if(!hasFocus()) 00042 { 00043 showHintText(); 00044 } 00045 else 00046 { 00047 hideHintText(); 00048 } 00049 } 00050 00051 void ALineEdit::showHintText() 00052 { 00053 if(this->text() == "") 00054 { 00055 this->setText(defaultText); 00056 setStyleSheet("QLineEdit{color: #BBBBBB;}"); 00057 } 00058 } 00059 00060 void ALineEdit::hideHintText() 00061 { 00062 if(this->text() == "" || this->text() == defaultText) 00063 { 00064 this->setText(QString()); 00065 setStyleSheet("QLineEdit{color: #000000;}"); 00066 } 00067 } 00068 00069 void ALineEdit::focusInEvent(QFocusEvent *e) 00070 { 00071 hideHintText(); 00072 QLineEdit::focusInEvent(e); 00073 } 00074 00075 void ALineEdit::focusOutEvent(QFocusEvent *e) 00076 { 00077 // Focus out: Switch to displaying the hint text unless 00078 // there is user input 00079 showHintText(); 00080 QLineEdit::focusOutEvent(e); 00081 }