Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Make only some part of text in QtextEdit Uneditable
Forum Updated to NodeBB v4.3 + New Features

Make only some part of text in QtextEdit Uneditable

Scheduled Pinned Locked Moved Qt for Python
8 Posts 3 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saeid0034
    wrote on last edited by saeid0034
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      What is the exact goal of that editing feature ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

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

        What is the exact goal of that editing feature ?

        S Offline
        S Offline
        saeid0034
        wrote on last edited by saeid0034
        #3

        @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?

        JonBJ 1 Reply Last reply
        0
        • S saeid0034

          @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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          S 2 Replies Last reply
          2
          • JonBJ JonB

            @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.

            S Offline
            S Offline
            saeid0034
            wrote on last edited by
            #5

            @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

            JonBJ 1 Reply Last reply
            0
            • S saeid0034

              @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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @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?

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • JonBJ JonB

                  @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.

                  S Offline
                  S Offline
                  saeid0034
                  wrote on last edited by
                  #8

                  @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)

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved