使用 QScintilla 进行多光标编辑 [英] Multi-Cursor editing with QScintilla

查看:34
本文介绍了使用 QScintilla 进行多光标编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个像 SublimeText 一样支持多光标编辑的小 QScintilla 小部件.据我所知 Scintilla 已经支持多个游标,但我还没有看到任何例子.

I'd like to create a little QScintilla widget supporting multi-cursor-editing like in SublimeText. As far as i know Scintilla already supports multiple cursors but I haven't seen any example out there.

那么,任何人都可以发布一个小例子来展示使用 QScintilla 的多个光标的基础知识吗?

So, could anyone please post a little example showing the basics of multiple cursors with QScintilla?

推荐答案

多光标功能在 Scintilla 中可用,但 QScintilla 不提供此功能的直接包装器.但是,您可以重新实现"您的包装器,因为几乎所有事情都可以使用 SendScintilla 方法完成.

The multi-cursor feature is available in Scintilla, but QScintilla doesn't provide direct wrappers for this feature. However, you can "reimplement" your wrappers, since almost everything can be done with the SendScintilla method.

from PyQt5.Qsci import QsciScintilla
from PyQt5.QtWidgets import QApplication

app = QApplication([])

ed = QsciScintilla()

ed.setText('insert <-\nsome <-\ntext <-\n')
ed.show()

# typing should insert in all selections at the same time
ed.SendScintilla(ed.SCI_SETADDITIONALSELECTIONTYPING, 1)

# do multiple selections
offset = ed.positionFromLineIndex(0, 7) # line-index to offset
ed.SendScintilla(ed.SCI_SETSELECTION, offset, offset)
# using the same offset twice selects no characters, hence a cursor

offset = ed.positionFromLineIndex(1, 5)
ed.SendScintilla(ed.SCI_ADDSELECTION, offset, offset)

offset = ed.positionFromLineIndex(2, 5)
ed.SendScintilla(ed.SCI_ADDSELECTION, offset, offset)

app.exec_()

您应该将 SendScintilla 调用包装在您自己的包装器中.

You should wrap the SendScintilla calls in your own wrappers.

请记住,offsets 以字节表示,因此取决于文本的编码,这或多或少被 QScintilla 的 QStrings 隐藏.另一方面,行索引"以字符表示(如果使用 unicode 则为代码点),因此更可靠.

Keep in mind that the offsets are expressed in bytes and thus depend on the encoding of the text, which is more or less hidden by QScintilla's QStrings. On the other hand, the "line-index" are expressed in characters (codepoints if using unicode), and thus are more reliable.

这篇关于使用 QScintilla 进行多光标编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆