Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Masking QLineEdit text

Tags:

pyqt4

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

like image 699
RKh Avatar asked Jan 11 '11 22:01

RKh


People also ask

How do I hide text in QLineEdit?

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.

How do I read text in QLineEdit?

As mentioned in the documentation, the text of a QLineEdit can be retrieved with its method text . Note that it's a QString , not a regular string, but that shouldn't be a problem as you format it with your "%s" % text .

How do I get text from QTextEdit?

QTextEdit does not have any text() method, if you want to get the text you must use toPlainText() , if you want to clean the text it is better to use clear() since it makes it more readable.

How do I get text from line edit in Qt?

You use QLineEdit::setText() to change the text and QLineEdit::text() to read. Your questions are very basic and show a clear lack of studying the documentation and experimenting thing yourself. Qt is one of the better documented frameworks out there and with lots of examples. Please take your time to go through them.


5 Answers

editor = QLineEdit()
editor.setEchoMode(QLineEdit.Password)
like image 51
Anti Earth Avatar answered Nov 16 '22 22:11

Anti Earth


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.

like image 42
Chinmay Kanchi Avatar answered Nov 16 '22 23:11

Chinmay Kanchi


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

like image 31
serge_gubenko Avatar answered Nov 16 '22 22:11

serge_gubenko


As docs say http://doc-snapshot.qt-project.org/4.8/stylesheet-examples.html#customizing-qlineedit:

The password character of line edits that have QLineEdit::Password echo mode can be set using:

QLineEdit[echoMode="2"] {
    lineedit-password-character: 9679;
}
like image 31
Senyai Avatar answered Nov 16 '22 23:11

Senyai


In Qt Designer

Select the line edit, and in the Property Editor window, there will be a property echoMode which you can set to Password.

Using python code

In this case, Anti Earth's answer will work which is:

myLineEdit.setEchoMode(QLineEdit.Password)
like image 22
Ali Sajjad Avatar answered Nov 16 '22 22:11

Ali Sajjad