Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to make Password Echo On Edit not clear out the Previous Content



  • I am trying to use Qlineedit password echo on edit so that it encrypts character by character but what is happing I have to focus out and when to focus back in and I type the password gets cleared. I want to b e able to type in *****b the b appears and then when I type another character like "a" the "b" gets encrypted , the password doesnt cleared and the input now looks like ******a. So far I have created a q timer that focuses out after a key has been entered and then focuses again at the timeout function but when another key is pressed the whole password gets encrypted. I read in the documentation that passwordEchoOnEedit clears out the previous content and is the only mode that does this



  • I have found out that I need to reimplement inputmethodEvent in my own version while inheriting QLineEdit so that it doesnt clear up however Its telling d_func is a private. How else can I access it. I need to access the d pointer. This is the function

    Q_D(QLineEdit);//<= this is where the error of that d is a private of QLineEdit
    if (d->control->isReadOnly()) {
    e->ignore();
    return;
    }
    if (echoMode() == PasswordEchoOnEdit && !d->control->passwordEchoEditing()) {
    // Clear the edit and reset to normal echo mode while entering input
    // method data; the echo mode switches back when the edit loses focus.
    // ### changes a public property, resets current content.
    d->updatePasswordEchoEditing(true);
    clear();//<= this is where it clears the password
    }
    #ifdef QT_KEYPAD_NAVIGATION
    // Focus in if currently in navigation focus on the widget
    // Only focus in on preedits, to allow input methods to
    // commit text as they focus out without interfering with focus
    if (QApplicationPrivate::keypadNavigationEnabled()
    && hasFocus() && !hasEditFocus()
    && !e->preeditString().isEmpty())
    setEditFocus(true);
    #endif
    d->control->processInputMethodEvent(e);
    #if QT_CONFIG(completer)
    if (!e->commitString().isEmpty())
    d->control->complete(Qt::Key_unknown);
    #endif



  • @TomQue as answered in the thread https://forum.qt.io/topic/125634/accessing-d-pointer-d_func-from-qlineedit-class/9

    My simplest solution for the modified behavior (not to clear the text content) of "PasswordEchoOnEdit" will be as follows.

    1)set the lineEdit "passwdEdit" echomode as "normal"
    2)connect the QApplication focusChanged signal to lambda function
    3)In lambda function check
    if last focus widget was "passwdEdit" then set echommode as "Password"
    else if Current focus is "passwdEdit" then set echommode as "Normal"

    tested code sample is as follows

    connect(qApp, &QApplication::focusChanged, [this](QWidget *oldFocus, QWidget *newFocus )
        {
            if(oldFocus == this->ui->passwdEdit)
               this->ui->passwdEdit->setEchoMode(QLineEdit::Password);
            else if(newFocus ==this->ui->passwdEdit)  
                this->ui->passwdEdit->setEchoMode(QLineEdit::Normal);
        }   );
    

Log in to reply