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

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?

    I'm running Kubuntu 20.04 with Qt version 5.18.5.


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



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



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


Log in to reply