Nominate our 2022 Qt Champions!

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:
    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";
    but with no avail.
    Could you help me, please

  • Moderators

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

    That should work.

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

    QString text=line.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