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

Replacing a character in a QLineEdit text



  • Hi,

    To simplify, I am using a QlineEdit line that contains : PRTU
    I would like to replace the character 'R' by ' S'.
    I tried :
    line.text().replace('R','S') But it seems not to work.
    I tried also:
    text=""
    if(line.text().contains('P') text=text+"P";
    if (line.text().contains('R') text=text+"S"
    if(line.text().contains('T') text=text+"T";
    if(line.text().contains('U') text=text+"U";
    line.setText(text);
    but with no avail.
    Could you help me, please


  • Moderators

    @
    QString text = line.text();
    text.replace("R", "S");
    line.setText(text);
    @

    That should work.



  • Thank you for your reply.
    As you advised I wrote:

    QString text=line.text();
    text.replace('R','S');
    line.setText(text);

    But the computer erases the charactere 'R' but put nothing instead.
    What do I do wrong?
    Thank you


  • Moderators

    I don't know. Code looks ok.



  • I found my mistake. I feel so stupid.
    I made the changes in a constructor window that it not read at the beginning.
    Well sorry for my stupidity.
    Thanks for your help.


  • Moderators

    No problem, we all make mistakes in our apps :)


Log in to reply