Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Show cursor in read-only QTextEdit
QtWS25 Last Chance

Show cursor in read-only QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.1k Views
  • 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.
  • LorenDBL Offline
    LorenDBL Offline
    LorenDB
    wrote on last edited by
    #1

    I have a widget that inherits QTextEdit which I want to be non-editable for the most part; however, I am intercepting key presses to edit the contents of the text box when certain conditions are met. The problem here is this: if I set the text box to be read-only, the cursor disappears; if I don't, calling QTextEdit::keyPressEvent will allow the user to edit the contents. Is there any way to make the cursor show up when the box is read-only?

    I'm running Kubuntu 20.04 with Qt version 5.18.5.

    jeremy_kJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @LorenDB said in Show cursor in read-only QTextEdit:

      Is there any way to make the cursor show up when the box is read-only?

      No, you have to paint it by yourself.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • LorenDBL Offline
        LorenDBL Offline
        LorenDB
        wrote on last edited by
        #3

        OK.

        I did experiment with calling setTextInteractionFlags(Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse), which shows a non-blinking cursor. That's not perfect, but it's better than no cursor at all.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I still don't understand why a read-only edit should show a (blinking) cursor at all but ... :)

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • LorenDBL Offline
            LorenDBL Offline
            LorenDB
            wrote on last edited by
            #5

            @Christian-Ehrlicher I want it to be editable, but only in the sense that my program takes care of the editing process, and I don't want the user to be able to delete any text.

            This program is intended to be a memorization aid where you type the first letter of each word of the content that you want to memorize and get instant feedback on whether you typed the right or wrong letter. In this case, I don't want the user deleting the text that they have correctly typed.

            jsulmJ 1 Reply Last reply
            0
            • LorenDBL LorenDB

              @Christian-Ehrlicher I want it to be editable, but only in the sense that my program takes care of the editing process, and I don't want the user to be able to delete any text.

              This program is intended to be a memorization aid where you type the first letter of each word of the content that you want to memorize and get instant feedback on whether you typed the right or wrong letter. In this case, I don't want the user deleting the text that they have correctly typed.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @LorenDB said in Show cursor in read-only QTextEdit:

              In this case, I don't want the user deleting the text that they have correctly typed.

              Then make it readonly after the user entered correct text.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • LorenDBL LorenDB

                I have a widget that inherits QTextEdit which I want to be non-editable for the most part; however, I am intercepting key presses to edit the contents of the text box when certain conditions are met. The problem here is this: if I set the text box to be read-only, the cursor disappears; if I don't, calling QTextEdit::keyPressEvent will allow the user to edit the contents. Is there any way to make the cursor show up when the box is read-only?

                I'm running Kubuntu 20.04 with Qt version 5.18.5.

                jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #7

                @LorenDB said in Show cursor in read-only QTextEdit:

                I have a widget that inherits QTextEdit which I want to be non-editable for the most part; however, I am intercepting key presses to edit the contents of the text box when certain conditions are met. The problem here is this: if I set the text box to be read-only, the cursor disappears; if I don't, calling QTextEdit::keyPressEvent will allow the user to edit the contents. Is there any way to make the cursor show up when the box is read-only?

                Override keyPressEvent() in the subclass and do the condition testing there. Don't pass the event to QTextEdit::keyPressEvent() unless the conditions are met.

                I'm running Kubuntu 20.04 with Qt version 5.18.5.

                Presumably this is a typo. 5.15 is the end of the Qt 5 series.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                LorenDBL 1 Reply Last reply
                1
                • jeremy_kJ jeremy_k

                  @LorenDB said in Show cursor in read-only QTextEdit:

                  I have a widget that inherits QTextEdit which I want to be non-editable for the most part; however, I am intercepting key presses to edit the contents of the text box when certain conditions are met. The problem here is this: if I set the text box to be read-only, the cursor disappears; if I don't, calling QTextEdit::keyPressEvent will allow the user to edit the contents. Is there any way to make the cursor show up when the box is read-only?

                  Override keyPressEvent() in the subclass and do the condition testing there. Don't pass the event to QTextEdit::keyPressEvent() unless the conditions are met.

                  I'm running Kubuntu 20.04 with Qt version 5.18.5.

                  Presumably this is a typo. 5.15 is the end of the Qt 5 series.

                  LorenDBL Offline
                  LorenDBL Offline
                  LorenDB
                  wrote on last edited by
                  #8

                  @jeremy_k said in Show cursor in read-only QTextEdit:

                  I'm running Kubuntu 20.04 with Qt version 5.18.5.

                  Presumably this is a typo. 5.15 is the end of the Qt 5 series.

                  Oops, 5.18.5 is my Plasma version. I'm on Qt 5.12.8.

                  Override keyPressEvent() in the subclass and do the condition testing there. Don't pass the event to QTextEdit::keyPressEvent() unless the conditions are met.

                  That's actually what I ended up doing.

                  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