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

Accessing d pointer d_func from QLineEdit class



  • I am trying to access the d pointer but the error that d_func is private for QLineEdit. I am having a hard time accessing it here is the code

    Q_D(QLineEdit);//<= this is where the error of that d is a private of QLineEdit
    if (d->control->isReadOnly()) {
    e->ignore();
    return;
    }


  • Moderators

    @TomQue said in Accessing d pointer d_func from QLineEdit class:

    I am trying to access the d pointer

    What is your goal?

    It is private, we are not supposed to access it.

    if (d->control->isReadOnly())

    Call the public function instead: if (this->isReadOnly())



  • I am trying to re-implement a function in QLineEdit so I change its behaviour and it requires I access the d pointer.


  • Moderators

    @TomQue said in Accessing d pointer d_func from QLineEdit class:

    I am trying to re-implement a function in QLineEdit so I change its behaviour

    Please describe the changed behaviour that you want to implement



  • It pertains to passwordEchoOnEdit. its the only mode that deletes the contents once you switch echo modes. The original code that of QLineEdit re-implements the function InputmethodEvent and forces it to clear. I am trying re-implement it using my own version of the function so it doesnt clear

    here is the original code
    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
    }
    }

    For to go through I need to access the d pointer


  • Lifetime Qt Champion

    You have to install and use the private headers. But then there is no BC guarantee on this usage anymore. It may not work anymore with a new Qt version (or an older).

    What exactly do you need from the d-pointer? QWidget::inputMethodEvent() is virtual so simply override it and call the base class (or not depending on your needs).



  • I need to access its members. like this one updatePasswordEchoEditing(true);


  • Lifetime Qt Champion

    As I said - you need to install the private headers and use them. And you need to recompile your program everytime Qt gets updated since private headers are subject to change every time.



  • @TomQue said in Accessing d pointer d_func from QLineEdit class:

    The original code that of QLineEdit re-implements the function InputmethodEvent and forces it to clear. I am trying re-implement it using my own version of the function so it doesnt clear

    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