关于QLineEdit右侧添加图标(点击查看密码,点击隐藏密码)如何优化图标的位置呢?
各位大佬,请问QLineEdit右侧添加图标,点击查看密码,点击隐藏密码时,为啥如下代码显示的右侧图标会向下拉长呢?有什么办法可以让它显示正常或好看一些吗?
具体代码如下:
class pwdLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit pwdLineEdit(QWidget *parent = nullptr);
~pwdLineEdit();
signals:
public slots:
};
//pwdLineEdit
pwdLineEdit::pwdLineEdit(QWidget *parent)
:QLineEdit(parent)
{
setStyleSheet("QLineEdit{background:transparent;border-image:url(:/res/pwd/lineEditBkg.png);border-style:solid;color:#003DA6;padding-left: +8px;border-color:#232423;font:11px ""SimHei"";}"
"QLineEdit:hover{border: 1px solid #014099;}"
"QLineEdit:focus{border: 1px solid #014099;}"
"QLineEdit:hover{border: 1px solid #014099;}"
"QLineEdit QPushButton {width: 16px;height: 16px;qproperty-flat: true;margin-right: 4px;border: none;border-width: 0;border-image: url(:/res/pwd/password_hide.png) 0 0 0 0 stretch stretch;background: transparent;}"
"QLineEdit QPushButton::checked {border-image: url(:/res/pwd/password_show.png) 0 0 0 0 stretch stretch;}");
setEchoMode(QLineEdit::Password);
QPushButton* button = new QPushButton();
button->setCursor(Qt::PointingHandCursor);
button->setCheckable(true);
connect(button, &QPushButton::toggled, [this](bool checked) {
if (checked)
{
setEchoMode(QLineEdit::Normal);
}
else
{
setEchoMode(QLineEdit::Password);
}
});
QHBoxLayout* layout = new QHBoxLayout();
layout->addStretch();
layout->addWidget(button);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
}
pwdLineEdit::~pwdLineEdit()
{
}
具体效果图如下:

具体代码如下:
class pwdLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit pwdLineEdit(QWidget *parent = nullptr);
~pwdLineEdit();
signals:
public slots:
};
//pwdLineEdit
pwdLineEdit::pwdLineEdit(QWidget *parent)
:QLineEdit(parent)
{
setStyleSheet("QLineEdit{background:transparent;border-image:url(:/res/pwd/lineEditBkg.png);border-style:solid;color:#003DA6;padding-left: +8px;border-color:#232423;font:11px ""SimHei"";}"
"QLineEdit:hover{border: 1px solid #014099;}"
"QLineEdit:focus{border: 1px solid #014099;}"
"QLineEdit:hover{border: 1px solid #014099;}"
"QLineEdit QPushButton {width: 16px;height: 16px;qproperty-flat: true;margin-right: 4px;border: none;border-width: 0;border-image: url(:/res/pwd/password_hide.png) 0 0 0 0 stretch stretch;background: transparent;}"
"QLineEdit QPushButton::checked {border-image: url(:/res/pwd/password_show.png) 0 0 0 0 stretch stretch;}");
setEchoMode(QLineEdit::Password);
QPushButton* button = new QPushButton();
button->setCursor(Qt::PointingHandCursor);
button->setCheckable(true);
connect(button, &QPushButton::toggled, [this](bool checked) {
if (checked)
{
setEchoMode(QLineEdit::Normal);
}
else
{
setEchoMode(QLineEdit::Password);
}
});
QHBoxLayout* layout = new QHBoxLayout();
layout->addStretch();
layout->addWidget(button);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
}
pwdLineEdit::~pwdLineEdit()
{
}
具体效果图如下:

...全文
相关推荐
pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法
今天小编就为大家分享一篇pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
QT 实现输入隐藏密码,并实现正则表达式
使用QT实现输入密码,先显示明文,后隐藏,并实现正则表达式验证密码
QT源代码,口令输入框,先显示后隐藏,简单,不用继承QlineEdit
QT工程源代码,实现口令输入框,先显示后隐藏,简单,不用继承QlineEdit
pyqt 实现QlineEdit 输入密码显示成圆点的方法
使用自带的函数就可以实现: lineEdit.setEchoMode(QLineEdit.Password) import struct from PyQt5.QtWidgets import (QMessageBox, QLineEdit) from PyQt5.QtGui import QPixmap {...} class LoginWindow(QtWidgets.QWidget): {...} def __init__(self): super(LoginWindow, self).__init__() # 加载ui文件 loadUi('login.ui
Qt:QLineEdit
关于Qt中QLineEdit的详解,参见博文:http://blog.csdn.net/rl529014/article/details/52060373


