继上一篇博客之后,再完善一个小功能,输入框添加隐藏、显示功能,效果图如下:
构造函数中添加:
pwShow = new QPushButton(this);
pwShow->setStyleSheet("border : none;");
QPixmap _pwShow(":/icon/eyesOpen.png");
QPixmap show_pixmap = _pwShow.scaled(30 , 30 , Qt::KeepAspectRatio);
pwShow->setIcon(show_pixmap);
pwShow->setCursor(Qt::PointingHandCursor);
pwShow->setToolTip("查看密码");
connect(pwShow , SIGNAL(clicked(bool)) , this , SLOT(showPassword()));
pwHide = new QPushButton(this);
pwHide->setStyleSheet("border : none;");
QPixmap _pwHide(":/icon/eyesClose.png");
QPixmap hide_pixmap = _pwHide.scaled(30 , 30 , Qt::KeepAspectRatio);
pwHide->setIcon(hide_pixmap);
pwHide->setCursor(Qt::PointingHandCursor);
pwHide->setToolTip("隐藏密码");
connect(pwHide , SIGNAL(clicked(bool)) , this , SLOT(hidePassword()));
QHBoxLayout* lineLayout = new QHBoxLayout();
lineLayout->addStretch();
lineLayout->addWidget(pwShow);
lineLayout->addWidget(pwHide);
lineLayout->setMargin(0);
lineLayout->setSpacing(0);
ui->password->setLayout(lineLayout);
ui->password->setTextMargins(1, 1, 1, 1);
pwShow->show();
pwHide->hide();
ui->password->setEchoMode(QLineEdit::Password); //设置自适应平台的密码掩饰字符
隐藏与显示函数:
void login::showPassword()
{
pwShow->hide();
pwHide->show();
ui->password->setEchoMode(QLineEdit::Normal); //默认无掩饰符
}
void login::hidePassword()
{
pwShow->show();
pwHide->hide();
ui->password->setEchoMode(QLineEdit::Password); //设置自适应平台的密码掩饰字符
}
欢迎大家指教,希望大家喜欢
路漫漫其修远兮,同志仍须努力