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