轉載:http://www.cnblogs.com/csuftzzk/p/qss_lineedit_completer.html?utm_source=tuicool&utm_medium=referral
轉載:http://blog.csdn.net/starcloud_zxt/article/details/5186489
轉載:http://blog.sina.com.cn/s/blog_a6fb6cc90101gu7w.html
一、窗口類.h中
用到的頭文件
#include <QStandardItemModel>
//自動補全 QStandardItemModel* m_Model ; QCompleter* m_completer;
void onEmailChoosed(const QString& email); void onTextChanged(const QString& str);
.cpp中
#include <QCompleter>
構造函數中 m_Model = new QStandardItemModel(0, 1, this); m_completer = new QCompleter(m_Model, this); ui.userEdit->setCompleter(m_completer); connect(m_completer, SIGNAL(activated(const QString&)), this, SLOT(onEmailChoosed(const QString&))); connect(ui.userEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
void QT_Test::onEmailChoosed(const QString& email) { ui.userEdit->clear(); // 清除已存在的文本更新內容 ui.userEdit->setText(email); } void QT_Test::onTextChanged(const QString& str) { if (str.contains("@")) // 如果已經輸入了@符號,我們就停止補全了。因為到了這一步,我們再補全意義也不大了。 { return; } QStringList strlist; strlist << "@163.com" << "@qq.com" << "@gmail.com" << "@hotmail.com" << "@126.com"; m_Model->removeRows(0, m_Model->rowCount()); // 先清楚已經存在的數據,不然的話每次文本變更都會插入數據,最后出現重復數據 for (int i = 0; i < strlist.size(); ++i) { m_Model->insertRow(0); m_Model->setData(m_Model->index(0, 0), str + strlist.at(i)); } }
二、Qt自定義密碼框,先顯示后隱藏
轉載:http://blog.csdn.net/caoshangpa/article/details/50978164
轉載:http://www.waitingfy.com/archives/1165
#ifndef QPASSWORDLINEEDIT_H #define QPASSWORDLINEEDIT_H #include <QLineEdit> class QPasswordLineEdit : public QLineEdit { Q_OBJECT public: //默認輸入300毫秒后消失 QPasswordLineEdit(QWidget *parent,int timeout = 300); ~QPasswordLineEdit(); //獲取真實的密碼 QString getPassword(); //設置顯示到隱藏的時間間隔 void setTimeout(int msec); private slots: void slotCursorPositionChanged(int,int); void slotTextEdited(const QString&); //顯示隱藏后的密碼 void slotDisplayMaskPassword(); private: //獲取隱藏后的密碼,這里為星號 QString getMaskPassword(); private: int mTimeout; QString mLineEditText; //到最后一個字符的長度 int mLastCharCount; }; #endif // QPASSWORDLINEEDIT_H
#include "qpasswordlineedit.h" #include <QTimer> #include <QDebug> QPasswordLineEdit::QPasswordLineEdit(QWidget *parent,int timeout) : QLineEdit(parent) { mTimeout = timeout; mLineEditText = ""; mLastCharCount = 0; connect(this,SIGNAL(cursorPositionChanged(int,int)),this,SLOT(slotCursorPositionChanged(int,int))); connect(this,SIGNAL(textEdited(const QString&)),this,SLOT(slotTextEdited(const QString&))); } QPasswordLineEdit::~QPasswordLineEdit() { } void QPasswordLineEdit::slotCursorPositionChanged(int oldPos,int newPos) { if(oldPos>=-1 && newPos>=0 ) { if(newPos>oldPos) { QTimer::singleShot(mTimeout,this,SLOT(slotDisplayMaskPassword())); } else { this->setCursorPosition(oldPos); } } } void QPasswordLineEdit::slotDisplayMaskPassword() { this->setText(getMaskPassword()); } void QPasswordLineEdit::slotTextEdited(const QString& text) { if(text.count()>mLastCharCount)//輸入 { mLineEditText.append(text.right(1)); } else if(text.count()<mLastCharCount)//刪除 { mLineEditText.remove(mLineEditText.count()-1,1); } mLastCharCount = mLineEditText.count(); } QString QPasswordLineEdit::getPassword() { return mLineEditText; } void QPasswordLineEdit::setTimeout(int msec) { mTimeout = msec; } QString QPasswordLineEdit::getMaskPassword() { QString mask = ""; int count = this->text().length(); if(count>0) { for(int i=0;i<count;i++) { mask += "*"; } } return mask; }
選中密碼編輯框,然后右鍵,在彈出菜單中選擇提升



點擊提升就完成了,是不是非常簡單
