如何在密码模式下更改QLineEdit项目符号的颜色

时间:2019-01-23 10:05:39

标签: python pyqt

我想进行“确认密码”行编辑,当密码不匹配时,其项目符号会变成红色。有没有办法改变子弹的颜色?

我当前的尝试是:

class CreateUserDialog(QDialog):

    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        layout.addSpacing(30)
        layout.addWidget(QLabel("Create a new account", self))

        self.username_edit = QLineEdit(parent=self)
        self.username_edit.setPlaceholderText("Username")
        layout.addWidget(self.username_edit)

        self.pwd_edit = QLineEdit(parent=self)
        self.pwd_edit.setPlaceholderText("Password")
        self.pwd_edit.setEchoMode(QLineEdit.Password)
        layout.addWidget(self.pwd_edit)

        self.confirm_pwd_edit = QLineEdit(parent=self)
        self.confirm_pwd_edit.setPlaceholderText("Confirm password")
        self.confirm_pwd_edit.setEchoMode(QLineEdit.Password)
        layout.addWidget(self.confirm_pwd_edit)

        self.pwd_edit.textChanged.connect(self.checkPasswordMatches)
        self.confirm_pwd_edit.textChanged.connect(
            self.checkPasswordMatches)

    def checkPasswordMatches(self, text):
        if not self.pwd_edit.text() == self.confirm_pwd_edit.text():
            palette = qt.QPalette()
            palette.setColor(qt.QPalette.Text, qt.Qt.red)
            self.confirm_pwd_edit.setPalette(palette)
        else:
            # restore normal style
            self.confirm_pwd_edit.setPalette(
                self.username_edit.palette())

这实际上适用于占位符文本,但不适用于用户开始键入时的项目符号。

1 个答案:

答案 0 :(得分:3)

您可以创建一个pyqtProperty来指示密码是否有效。

from PyQt5 import QtCore, QtGui, QtWidgets

qss = '''
ValidationLineEdit[echoMode="2"][isValid="true"] {
    color: green;
}
ValidationLineEdit[echoMode="2"][isValid="false"] {
    color: red;
}
'''

class ValidationLineEdit(QtWidgets.QLineEdit):
    valid_length_changed = QtCore.pyqtSignal(int)

    def __init__(self, *args, **kwargs):
        valid_length = 0
        if kwargs.get("valid_length"):
            valid_length = kwargs.pop("valid_length")
        super(ValidationLineEdit, self).__init__(*args, **kwargs)
        self._valid_length_ = valid_length
        self.textChanged.connect(self.update_stylesheet)

    @QtCore.pyqtProperty(bool)
    def isValid(self):
        if self.valid_length == 0: return False
        return len(self.text()) == self.valid_length

    def update_stylesheet(self):
        self.style().unpolish(self)
        self.style().polish(self)

    def valid_length(self):
        return self._valid_length_

    def set_valid_length(self, lenght):
        if self._valid_length_ == lenght: return
        self._valid_length_ = lenght
        self.valid_length_changed.emit(lenght)

    valid_length = QtCore.pyqtProperty(int, fget=valid_length, fset=set_valid_length, notify=valid_length_changed)


class CreateUserDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(CreateUserDialog, self).__init__(parent)

        self.username_edit = QtWidgets.QLineEdit(placeholderText="Username")
        self.pwd_edit = QtWidgets.QLineEdit(placeholderText="Password", echoMode=QtWidgets.QLineEdit.Password)
        self.confirm_pwd_edit = ValidationLineEdit(placeholderText="Confirm password", echoMode=QtWidgets.QLineEdit.Password)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addSpacing(30)
        lay.addWidget(QtWidgets.QLabel("Create a new account"))
        lay.addWidget(self.username_edit)
        lay.addWidget(self.pwd_edit)
        lay.addWidget(self.confirm_pwd_edit)

        self.pwd_edit.editingFinished.connect(self.on_editingFinished)

    @QtCore.pyqtSlot()
    def on_editingFinished(self):
        self.confirm_pwd_edit.valid_length = len(self.pwd_edit.text())

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setStyleSheet(qss)
    w =  CreateUserDialog()
    w.show()
    sys.exit(app.exec_())