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

Make only some part of text in QtextEdit Uneditable



  • Hello, I want to create a simple text editor with Pyside2 (or maybe c++ but Im perfer Python)
    the Idea is editor read text line by line from a txt file and show them in a QTextEdit, then user can edit it and then go to next line
    But I want the user not to be able to edit some part of text in QTextEdit
    I show some example

    Customer Name:$Name
    Age: $Age
    Phone Number: $Number
    

    I want to user be able to freely edit all text part except those part after $
    this is possible in QTextEdit? and if no there is any possible workaround?


  • Lifetime Qt Champion

    Hi,

    I do not see an easy way do to that with QTextEdit.

    What is the exact goal of that editing feature ?



  • @SGaist thanks for reply
    I have some text that I want user be able to edit them; but in other hand I need to user don't be able to edit some static part of it
    for example in following example user should only be able to edit "what is your name? - my name is" and "nice to meet you" part

    what is your name? - my name is $code. nice to meet you
    

    now that its not possible with only QTextEdit there is any other way?



  • @saeid0034
    If you attach a QRegularExpressionValidator to a QTextEdit/QLineEdit and assign it to require the bit you want to keep --- like .*\$code.* --- that is probab;ly the best you can do to enforce retaining what you want.



  • @JonB thank you, it's could be a workaround for it but problem is with this method I only can tell the user that you remove some code (and maybe tell him to fix it) and can't actually stop the user from removing codes



  • @saeid0034
    Yes, and that's the best you are going to be able to do, without some major rewriting of key presses inside a QTextEdit, or splitting it into 3 separate segments in distinct widgets....

    Have you actually tried what I suggested? I have not; but when you use a validator for a number it actually stops you typing unacceptable characters, I don't know whether the regular expression one might stop the user from getting rid of $code in yours?


  • Lifetime Qt Champion

    Hi,

    You might want to consider a different approach like providing a set of line edits that allows to change the specified text or maybe a list view / table view. Or if you want to show the whole text, have the text edit in read only mode and provide a dedicated editor when double clicking on the text that can be changed.



  • @JonB after some search I think its a good way to use it but sadly i cant find any good example of it in python (I can prove a set of line edits for every line, because some lines are so big and its make the program hard to work with)


Log in to reply