Masking QLineEdit text

后端 未结 5 999
灰色年华
灰色年华 2021-02-06 11:53

I am using PyQt4 QLineEdit widget to accept password. There is a setMasking property, but not following how to set the masking character.<

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 12:33

    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

提交回复
热议问题