简体   繁体   English

如何在QPlainTextEdit中将一行设置为只读?

[英]How to make a line readonly in QPlainTextEdit?

I would like to make particular lines readonly in a subclassed QPlainTextEdit. 我想在子类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). 我知道我可以覆盖keyPressed事件并在只读行上忽略它,但是我需要过滤非编辑键(例如箭头导航键)的键事件。 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? 是否有对此的内置支持或更好的实现方式?

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 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 http://doc.qt.io/qt-5/qtwidgets-richtext-syntaxhighlighter-example.html

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM