上次写的那个虽然勉强能用,但是客户说啦,这个框子呀,和真正的IRC聊天框还有所不同,于是我就重写了一下。
这次写的只用一个QList,应该是进一步减少了内存的占用,呵呵。
/* A new LineEdit that like IRC chat*/
class QIRCLineEdit : public QLineEdit
{
Q_OBJECT
public:
QIRCLineEdit();
private:
void keyPressEvent(QKeyEvent *);
//QList<QString> m_Inputlist1;//Stores the inputed strings,up list.
//QList<QString> m_Inputlist2;//Stores the inputed strings,down list.
QList<QString> m_Inputlist;
quint16 listindex;
//QString m_Currentline;//Stores a copy of the current text in the LineEdit.
public slots:
void myTextEdited();
void myclear();
};
QIRCLineEdit :: QIRCLineEdit ()
{
connect(this,SIGNAL(textEdited(QString)),this,SLOT(myTextEdited()));
listindex=0;
}
void QIRCLineEdit::keyPressEvent(QKeyEvent *e)
{
if(e->key() == Qt::Key_Up) {
if(text()==""){
if(m_Inputlist.empty()){
}else{
listindex--;
setText(m_Inputlist[listindex]);
}
}else if(text()!=""){
//m_Inputlist2.append(text());
if(listindex<=m_Inputlist.count()){
//m_Inputlist.append(text());
if(listindex==0){
setText(m_Inputlist[0]);
}else{
if(listindex==m_Inputlist.count()) m_Inputlist.append(text());
listindex--;
setText(m_Inputlist[listindex]);
}
}else if(listindex>m_Inputlist.count()){
if(!m_Inputlist.empty()){
m_Inputlist.append(text());
setText(m_Inputlist[listindex--]);
}
}
}
}else if(e->key() == Qt::Key_Down) {
if(text()==""){
if(m_Inputlist.empty()){
}else{
listindex++;
setText(m_Inputlist[listindex]);
}
}else if(text()!=""){
if(listindex+1<m_Inputlist.count()){
listindex++;
setText(m_Inputlist[listindex]);
}else if(listindex>=m_Inputlist.count()){
}
}
}else{
QLineEdit::keyPressEvent(e);
}
}
void QIRCLineEdit::myTextEdited()
{
if(m_Inputlist.count()==0){
listindex=1;
}else{
listindex=m_Inputlist.count();
}
}
void QIRCLineEdit::myclear()
{
if(text()!=""){
m_Inputlist.append(text());
if(m_Inputlist.count()==0){
listindex=1;
}else{
listindex=m_Inputlist.count();
}
}
QLineEdit::clear();
}