各位大佬,请问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()
{
}
具体效果图如下: