0

I created a QlineEdit, the content of which I would like to mask, I resorted to the QLineEdit.Password EchoMode, but unfortunately it doesn't let me specify the mask character. I stumbled upon the lineedit-password-character StyleSheet proprety, but it does nothing.

password_field.setEchoMode(QtWidgets.QLineEdit.Password)
password_field.setStyleSheet("color: #88c0d0; border: 0; lineedit-password-character: 2731; selection-background-color: #88c0d0; selection-color: #2e3440;")

The mask character I want is *

# Password field
password_field = QtWidgets.QLineEdit(window)
password_field.resize(input_window.width() - password_label.width() - input_field_offset - input_window_padding * 2, password_label.height())
password_field.move(password_label.x() + password_label.width() + input_field_offset, password_label.y())
password_field.setEchoMode(QtWidgets.QLineEdit.Password)
password_field.setStyleSheet("color: #88c0d0; border: 0; lineedit-password-character: 61; selection-background-color: #88c0d0; selection-color: #2e3440;")
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
  • What happens if you change the requested character to something like 61 (i.e. the '=' character)? Does that work? It may just be that your fonts are incomplete. – G.M. Dec 17 '22 at 19:22
  • Still the same. I'll edit the original post to show you how the code is looking – Assaad El Oualji Dec 17 '22 at 19:54
  • this doesn't seem like it would be very difficult to implement yourself. Why not just give it a try? – Alexander Dec 17 '22 at 21:04
  • Any advice on where to start, any information would be greatly appreciated. – Assaad El Oualji Dec 17 '22 at 21:06
  • It is defined by [QStyle::SH_LineEdit_PasswordCharacter](https://codebrowser.dev/qt5/qtbase/src/widgets/widgets/qlineedit.cpp.html#2287), see https://stackoverflow.com/questions/4663207/masking-qlineedit-text – char101 Dec 18 '22 at 09:23
  • The correct value to use for `*` (U+2731 HEAVY ASTERISK) is `10033`: i.e. you must use a *decimal integer*, since the stylesheet syntax doesn't understand hex values. – ekhumoro Dec 18 '22 at 14:38
  • @AssaadElOualji I cannot reproduce this. When using the correct values, the password characters are always displayed as expected. What platform are you testing on? I am using Qt-5.15.7 on Linux with a DejaVu Sans font. Note that the password character is a only a *hint*, so there's no guarantee that the platform style will actually use it. – ekhumoro Dec 18 '22 at 14:42
  • Setting the value to 10033 did not work. I am testing on Windows 10, and am using PyQt5-5.15.4 with the Roboto Mono font. – Assaad El Oualji Dec 18 '22 at 20:48
  • @AssaadElOualji Try setting a proxy-style [as suggested here](https://stackoverflow.com/a/4665230/984421) (but note that the reimplemented `styleHint` should return an integer rather than a string). If that doesn't change anything, it would imply that the style-hint is completely ignored on your system (or maybe Windows generally - I can't test that myself). – ekhumoro Dec 19 '22 at 13:37

1 Answers1

0

You need to specify the ASCII value for '*' which is 42. This worked for me.

password_field.setStyleSheet("color: #88c0d0; border: 0; lineedit-password-character: 42; selection-background-color: #88c0d0; selection-color: #2e3440;")
Gurushant
  • 952
  • 6
  • 23