Masking QLineEdit text

editor = QLineEdit()
editor.setEchoMode(QLineEdit.Password)

There is no setMasking property for QLineEdit in either PyQt4 or Qt4. Are you talking about setInputMask()? If you are, this does not do what you seem to think it does. It sets the mask against which to validate the input.

To get the control to hide what is typed, use the setEchoMode() method, which will (should) display the standard password hiding character for the platform. From what I can see from the documentation, if you want a custom character to be displayed, you will need to derive a new class. In general however, this is a bad idea, since it goes against what users expect to see.


It's quite easy using Qt: you would need to define a new style and return new character from the styleHint method whenever QStyle::SH_LineEdit_PasswordCharacter constant is queried. Below is an example:

class LineEditStyle : public QProxyStyle
{
public:
    LineEditStyle(QStyle *style = 0) : QProxyStyle(style) { }

    int styleHint(StyleHint hint, const QStyleOption * option = 0,
                  const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const
    {
        if (hint==QStyle::SH_LineEdit_PasswordCharacter)
            return '%';
        return QProxyStyle::styleHint(hint, option, widget, returnData);
    }
};

lineEdit->setEchoMode(QLineEdit::Password);
lineEdit->setStyle(new LineEditStyle(ui->lineEdit->style()));

now the problem is that pyqt doesn't seem to know anything about QProxyStyle; it seem to be not wrapped there, so you're stuck, unless you would want to wrap it yourself.

regards

Tags:

Pyqt4