Qt 密码框不可选中、复制、粘贴、无右键菜单等
生活随笔
收集整理的這篇文章主要介紹了
Qt 密码框不可选中、复制、粘贴、无右键菜单等
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在做用戶登錄、修改密碼的時(shí)候,往往會(huì)用到密碼框,其中一些功能要求與普通的輸入框不同。
例如:不能選中、復(fù)制、粘貼、無(wú)右鍵菜單等功能,當(dāng)然設(shè)置密碼不可見(jiàn)是必須的!
一般的密碼框:(默認(rèn) 可以選中,復(fù)制,粘貼,有右鍵菜單)
QLineEdit *pCommonLineEdit = new QLineEdit(this); pCommonLineEdit->setPlaceholderText(QStringLiteral("密碼由字母、數(shù)字、下劃線組成,長(zhǎng)度8-16位")); pCommonLineEdit->setEchoMode(QLineEdit::Password);
下面進(jìn)行一些設(shè)置:不可選擇,沒(méi)有右鍵菜單
1.可以進(jìn)行事件重寫(xiě)去完成
2.通過(guò)事件過(guò)濾器去實(shí)現(xiàn)上述的功能
QLineEdit *pFilterLineEdit = new QLineEdit(this); pFilterLineEdit->installEventFilter(new EventFilter(this)); pFilterLineEdit->setEchoMode(QLineEdit::Password); pFilterLineEdit->setPlaceholderText(QStringLiteral("密碼由字母、數(shù)字、下劃線組成,長(zhǎng)度8-16位")); pFilterLineEdit->setContextMenuPolicy(Qt::NoContextMenu); pFilterLineEdit->setMaxLength(16);class EventFilter : public QObject { public:explicit EventFilter(QObject *parent = 0);~EventFilter();protected:virtual bool eventFilter(QObject *obj, QEvent *event); }; EventFilter::EventFilter(QObject *parent): QObject(parent) {}EventFilter::~EventFilter() {}bool EventFilter::eventFilter(QObject *obj, QEvent *event) {QLineEdit *pLineEdit = qobject_cast<QLineEdit *>(obj);if (pLineEdit != NULL){switch (event->type()){case QEvent::MouseMove:case QEvent::MouseButtonDblClick:return true;case QEvent::KeyPress:{QKeyEvent *pKeyEvent = static_cast<QKeyEvent*>(event);if(pKeyEvent->matches(QKeySequence::SelectAll)|| pKeyEvent->matches(QKeySequence::Copy)|| pKeyEvent->matches(QKeySequence::Paste)){return true;}}}}return QObject::eventFilter(obj, event); }
上面就是三種關(guān)于密碼框的一些操作,基本也夠用了!
總結(jié)
以上是生活随笔為你收集整理的Qt 密码框不可选中、复制、粘贴、无右键菜单等的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringBoot学习笔记(一)整合M
- 下一篇: Laravel中构造方法中不能写retu