日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Qt 密码框不可选中、复制、粘贴、无右键菜单等

發(fā)布時(shí)間:2025/3/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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ě)去完成

class PasswordLineEdit : public QLineEdit {Q_OBJECTpublic:explicit PasswordLineEdit(QWidget *parent = 0);~PasswordLineEdit();protected:virtual void keyPressEvent(QKeyEvent *event);virtual void mouseMoveEvent(QMouseEvent *event);virtual void mouseDoubleClickEvent(QMouseEvent *event); }; PasswordLineEdit::PasswordLineEdit(QWidget *parent): QLineEdit(parent) {//設(shè)置無(wú)右鍵菜單setContextMenuPolicy(Qt::NoContextMenu);//設(shè)置無(wú)輸入時(shí)密碼提示setPlaceholderText(QStringLiteral("請(qǐng)輸入密碼"));//設(shè)置密碼隱藏setEchoMode(QLineEdit::Password);//設(shè)置最大長(zhǎng)度16位setMaxLength(16); }PasswordLineEdit::~PasswordLineEdit() { }//屏蔽輸入框全選、復(fù)制、粘貼功能 void PasswordLineEdit::keyPressEvent(QKeyEvent *event) {if(event->matches(QKeySequence::SelectAll)|| event->matches(QKeySequence::Copy)|| event->matches(QKeySequence::Paste)){return;}QLineEdit::keyPressEvent(event); }//鼠標(biāo)位于密碼框中時(shí)不可移動(dòng) void PasswordLineEdit::mouseMoveEvent(QMouseEvent *event) {Q_UNUSED(event); }//鼠標(biāo)位于密碼框中時(shí)不可移動(dòng) void PasswordLineEdit::mouseDoubleClickEvent(QMouseEvent *event) {Q_UNUSED(event); }


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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。