1

I would like to make particular lines readonly in a subclassed QPlainTextEdit.

I know I could override the keyPressed event and ignore it on the lines to be readonly, but I would need to filter the key event for non-editing keys (such as arrow navigation keys). Also, this would not account for text being pasted on that line.

Is there any built-in support for this or a better way of doing it?

Alan Spark
  • 8,152
  • 8
  • 56
  • 91
  • Remember that editing isn't done only using keys. It's also done using mouse, redo/undo, copy-paste, ... It seems to me that you'd need to modify Qt to add the desired functionality. – Kuba hasn't forgotten Monica Oct 13 '15 at 21:29

1 Answers1

1

If you keep track of the beginning and end of the "read-only" section, you could simply change the read-only attribute of the whole document when the cursor or part of a selection enters the read only range.

http://doc.qt.io/qt-5/qplaintextedit.html#readOnly-prop

I would use the syntax highlighter to make the read-only section a different color so it makes more sense to the end user.

http://doc.qt.io/qt-5/qtwidgets-richtext-syntaxhighlighter-example.html

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • This is not sufficient. Think of what happens when you attempt to cut a selection that includes one or more r/o sections. I'm not sure that hacks that don't touch Qt code will work :( – Kuba hasn't forgotten Monica Oct 13 '15 at 21:29
  • Yes, I was wondering about that too. Is there a way to detect a cut / paste (or just anything that is going to cause a text change)? – Alan Spark Oct 14 '15 at 17:47