Important: Please read the Qt Code of Conduct -

QTextEdit: adding normal text after html

  • Hi,
    if I put a span in QTextEdit using

    cursor.insertHtml("<span style='background-color:" + color + ";display:block'>&nbsp;</span>");

    and then I try to write after it interactively (NOT using code, as cursor.insertText()), new spans are added instead of inserting the actual content I enter using the keyboard.

    How can I handle this? Is there a function to restore the normal text after putting the HTML?
    Thanks in advance

  • @enne9 Can someone please help me?

  • Lifetime Qt Champion


    Please show some patience and allow at least 24 hours before bumping your own thread. This forum is run by volunteers that may not live in the same time zone as you.

    Il any case, please provide a minimal compilable example that allows to reproduce your use case.

    You should also add which version of Qt you are using and on what platform.

  • Qt version 5.14.1 on Linux platform.

    About the example, it is nothing specific, it can be reproduced by using a common QTextEdit and adding the line of code I've written before. Then run the application and type whatever character (from the keyboard) right after the HTML character introduced before.

    Let me know if there is something I didn't explain well.

  • @enne9
    You do not explain what it is you see, or how you say the HTML ends up which you should show us, after you enter some characters. I don't know what "new spans are added instead of inserting the actual content I enter using the keyboard" means.

    If I try your code (Linux Qt 5.12.2), with color being red, what I see is: a red background on one space, with the cursor immediately to the right of that space. When I type characters they appear, but with the red background. Doubtless because it does not know whether your cursor "position" is after the &nbsp; but still before the </span> versus whether it is after the </span> .

    If I append a <br /> to your HTML, "...>&nbsp;</span><br \>", the cursor starts at the line below and characters now typed do not have the red background.

    You may have to explain exactly what happens to you and what you want/expect.

Log in to reply