欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt實現(xiàn)密碼框

 更新時間:2022年06月14日 14:18:04   作者:憶秋年jd  
這篇文章主要為大家詳細介紹了Qt實現(xiàn)密碼框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Qt實現(xiàn)密碼框的具體代碼,供大家參考,具體內(nèi)容如下

密碼輸入框

支持無可選擇,不可復(fù)制,粘貼,可查看密碼,全清除功能

環(huán)境

Qt5.6.2+ Vs2013

效果

代碼

QPasswdLineEdit類

PasswdLineEdit.h

#ifndef PASSWDLINEEDIT_H
#define PASSWDLINEEDIT_H

#include <QLineEdit>
#include <QPushButton>

class QPasswdLineEdit : public QLineEdit
{
?? ?Q_OBJECT

public:
?? ?explicit QPasswdLineEdit(QWidget *parent = nullptr);
?? ?~QPasswdLineEdit();

?? ?void setCopyAble(bool able);

?? ?void setSelection(bool able);

?? ?void setContextMenu(bool able);

protected:
?? ?bool eventFilter(QObject *watched, QEvent *event);

?? ?private slots:
?? ?void slot_textChanged(const QString& text);

?? ?void slot_show();

?? ?void slot_hide();

?? ?void slot_clear();

private:
?? ?QPushButton* x_pBtnShow;
?? ?QPushButton* x_pBtnHide;
?? ?QPushButton* x_pBtnClear;

?? ?bool x_bCopy;?? ?//能否復(fù)制黏貼
?? ?bool x_bSelection; ?//能否能選中
?? ?bool x_bContenx; ?//是否存在右鍵菜單

?? ?bool x_bShow;
};

#endif // PASSWDLINEEDIT_H

PasswdLineEdit.cpp

#include "PasswdLineEdit.h"

#include <QEvent>
#include <QKeyEvent>
#include <QPainter>
#include <QHBoxLayout>
#include <QSize>

QPasswdLineEdit::QPasswdLineEdit(QWidget *parent)
?? ?: QLineEdit(parent)
?? ?, x_pBtnShow(nullptr)
?? ?, x_pBtnHide(nullptr)
?? ?, x_pBtnClear(nullptr)
?? ?, x_bCopy(false)
?? ?, x_bSelection(false)
?? ?, x_bContenx(false)
?? ?, x_bShow(false)
{
?? ?x_pBtnShow = new QPushButton(this);
?? ?x_pBtnHide = new QPushButton(this);
?? ?x_pBtnClear = new QPushButton(this);

?? ?setStyleSheet("QPushButton{border:none;}");

?? ?QPixmap _pixClear(":/image/clear");
?? ?x_pBtnClear->setIcon(_pixClear);
?? ?x_pBtnClear->setIconSize(_pixClear.size());
?? ?x_pBtnClear->setCursor(Qt::PointingHandCursor);
?? ?x_pBtnClear->setToolTip(QString::fromLocal8Bit("清理"));

?? ?QPixmap _pixShow(":/image/show");
?? ?x_pBtnShow->setIcon(_pixShow);
?? ?x_pBtnShow->setIconSize(_pixShow.size());
?? ?x_pBtnShow->setCursor(Qt::PointingHandCursor);
?? ?x_pBtnShow->setToolTip(QString::fromLocal8Bit("查看密碼"));

?? ?QPixmap _pixHide(":/image/hide");
?? ?x_pBtnHide->setIcon(_pixHide);
?? ?x_pBtnHide->setIconSize(_pixHide.size());
?? ?x_pBtnHide->setCursor(Qt::PointingHandCursor);
?? ?x_pBtnHide->setToolTip(QString::fromLocal8Bit("隱藏密碼"));

?? ?QHBoxLayout* _pHLayout = new QHBoxLayout();

?? ?_pHLayout->addStretch();
?? ?_pHLayout->addWidget(x_pBtnShow);
?? ?_pHLayout->addWidget(x_pBtnHide);
?? ?_pHLayout->addWidget(x_pBtnClear);

?? ?_pHLayout->setMargin(0);
?? ?_pHLayout->setSpacing(0);

?? ?x_pBtnShow->hide();
?? ?x_pBtnHide->hide();
?? ?x_pBtnClear->hide();

?? ?this->setLayout(_pHLayout);

?? ?this->setTextMargins(1, 1, 1, 1);

?? ?setFixedHeight(30);

?? ?//密碼顯示模式
?? ?setEchoMode(QLineEdit::Password);

?? ?if (x_bContenx)
?? ?{
?? ??? ?this->setContextMenuPolicy(Qt::DefaultContextMenu);
?? ?}
?? ?else
?? ?{
?? ??? ?this->setContextMenuPolicy(Qt::NoContextMenu);
?? ?}

?? ?this->installEventFilter(this);

?? ?connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slot_textChanged(const QString&)));

?? ?connect(x_pBtnShow, SIGNAL(clicked()), this, SLOT(slot_show()));
?? ?connect(x_pBtnHide, SIGNAL(clicked()), this, SLOT(slot_hide()));
?? ?connect(x_pBtnClear, SIGNAL(clicked()), this, SLOT(slot_clear()));
}

QPasswdLineEdit::~QPasswdLineEdit()
{

}

void QPasswdLineEdit::setCopyAble(bool able)
{
?? ?x_bCopy = able;
}

void QPasswdLineEdit::setSelection(bool able)
{
?? ?x_bSelection = able;
}

void QPasswdLineEdit::setContextMenu(bool able)
{
?? ?x_bContenx = able;

?? ?if (x_bContenx)
?? ?{
?? ??? ?this->setContextMenuPolicy(Qt::DefaultContextMenu);
?? ?}
?? ?else
?? ?{
?? ??? ?this->setContextMenuPolicy(Qt::NoContextMenu);
?? ?}
}


bool QPasswdLineEdit::eventFilter(QObject *watched, QEvent *event)
{
?? ?QPasswdLineEdit* _pObj = qobject_cast<QPasswdLineEdit*>(watched);

?? ?if (_pObj == this)
?? ?{
?? ??? ?switch (event->type())
?? ??? ?{
?? ??? ?case QEvent::MouseMove:
?? ??? ?case QEvent::MouseButtonDblClick:
?? ??? ??? ?return !x_bSelection;
?? ??? ??? ?break;
?? ??? ?case QEvent::MouseButtonPress:
?? ??? ??? ?{
?? ??? ??? ??? ?QMouseEvent* _pMouseEvent = static_cast<QMouseEvent*>(event);

?? ??? ??? ??? ?if (_pMouseEvent->button() == Qt::RightButton)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?return !x_bContenx;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?break;
?? ??? ?default:
?? ??? ??? ?break;
?? ??? ?}
?? ?}

?? ?return QLineEdit::eventFilter(watched, event);
}

void QPasswdLineEdit::slot_textChanged(const QString& text)
{
?? ?if (!text.isEmpty())
?? ?{
?? ??? ?setTextMargins(1, 1, 36, 1);

?? ??? ?if (x_bShow)
?? ??? ?{
?? ??? ??? ?x_pBtnShow->hide();
?? ??? ??? ?x_pBtnHide->show();
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?x_pBtnShow->show();
?? ??? ??? ?x_pBtnHide->hide();
?? ??? ?}

?? ??? ?x_pBtnClear->show();
?? ?}
?? ?else
?? ?{
?? ??? ?x_pBtnShow->hide();
?? ??? ?x_pBtnHide->hide();
?? ??? ?x_pBtnClear->hide();

?? ??? ?x_bShow = false;
?? ??? ?setEchoMode(QLineEdit::Password);

?? ??? ?this->setTextMargins(1, 1, 1, 1);
?? ?}
}

void QPasswdLineEdit::slot_show()
{
?? ?x_bShow = true;

?? ?setEchoMode(QLineEdit::Normal);

?? ?x_pBtnShow->hide();
?? ?x_pBtnHide->show();
}

void QPasswdLineEdit::slot_hide()
{
?? ?setEchoMode(QLineEdit::Password);

?? ?x_pBtnShow->show();
?? ?x_pBtnHide->hide();
}

void QPasswdLineEdit::slot_clear()
{
?? ?clear();
}

使用

x_pPsdEdit = new QPasswdLineEdit(this);
QGridLayout* _pGLayout = new QGridLayout();
_pGLayout->addWidget(x_pPsdEdit);
setLayout(_pGLayout);

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • opencv實現(xiàn)讀取視頻保存視頻

    opencv實現(xiàn)讀取視頻保存視頻

    這篇文章主要為大家詳細介紹了opencv實現(xiàn)讀取視頻保存視頻,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C 語言二叉樹幾種遍歷方法詳解及實例

    C 語言二叉樹幾種遍歷方法詳解及實例

    這篇文章主要介紹了C 語言二叉樹幾種遍歷方法詳解及實例的相關(guān)資料,二叉樹在數(shù)據(jù)結(jié)構(gòu)當中是非常重要的知識要點,這里對二叉樹進行了總結(jié),需要的朋友可以參考下
    2017-01-01
  • C++ Vector 動態(tài)數(shù)組的實現(xiàn)

    C++ Vector 動態(tài)數(shù)組的實現(xiàn)

    這篇文章主要介紹了C++ Vector 動態(tài)數(shù)組的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • C語言實現(xiàn)哈夫曼樹的構(gòu)建

    C語言實現(xiàn)哈夫曼樹的構(gòu)建

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)哈夫曼樹的構(gòu)建,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C/C++語言printf命令使用方法

    C/C++語言printf命令使用方法

    在本篇文章里小編給大家分享了關(guān)于C/C++語言printf命令使用方法和步驟,對此有需要的朋友們學習下。
    2019-01-01
  • OpenCV實現(xiàn)簡單錄屏功能

    OpenCV實現(xiàn)簡單錄屏功能

    這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)簡單錄屏功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C語言詳盡圖解函數(shù)棧幀的創(chuàng)建和銷毀實現(xiàn)

    C語言詳盡圖解函數(shù)棧幀的創(chuàng)建和銷毀實現(xiàn)

    我們知道c語言中函數(shù)都是被調(diào)用的,main函數(shù)里面能調(diào)用其他函數(shù),其實main函數(shù)也是被別的函數(shù)調(diào)用的,下面通過本文給大家分享c語言函數(shù)棧幀的創(chuàng)建和銷毀過程,一起看看吧
    2022-05-05
  • C語言函數(shù)的基本使用和遞歸小結(jié)

    C語言函數(shù)的基本使用和遞歸小結(jié)

    這篇文章主要介紹了C語言函數(shù)的基本使用和遞歸小結(jié),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • C++實現(xiàn)重載矩陣的部分運算符

    C++實現(xiàn)重載矩陣的部分運算符

    這篇文章主要為大家詳細介紹了如何利用C++實現(xiàn)重載矩陣的部分運算符,文中的示例代碼講解詳細,對我們學習C++有一定幫助,需要的可以參考一下
    2022-10-10
  • c++遍歷lua table示例

    c++遍歷lua table示例

    這篇文章主要介紹了c++遍歷lua table示例,需要的朋友可以參考下
    2014-04-04

最新評論