我正在为富文本使用qt5 TextEdit in Gambas3。
请考虑守则:
Dim cursorpos As Integer
If Key.Code = Key.Left Or Key.Code = Key.Up Or Key.code = Key.Right Or Key.Code = Key.Down Or Key.Code = Key.Delete Or Key.Code = Key.Backspace Then
cursorpos = TextEdit1.Pos ' just pick the position
Print cursorpos
Else
cursorpos = TextEdit1.Pos
Print cursorpos
TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" ' this preserves the newlines, and replaces them with a <br> for the rich text
Print "setting : ", cursorpos ' prints the correct value
TextEdit1.Pos = cursorpos ' does not work
Print "got : ", TextEdit1.Pos ' jumps to the end of the string
Endif现在,我写道:
This si a line
this is a second line我在第一行有个错误。我用我的箭钥匙到达那里。我点击了两次backspace,并删除了单词si。一切都很好。现在我希望输入字符i,光标应该停留在字符i之后,但是一旦输入了正确的位置,光标就会跳到文本的末尾。
请帮帮忙。如何将光标位置保持在正确的位置?谢谢。
发布于 2022-10-19 02:02:57
你的错误是这一行..。
TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>"
' this preserves the newlines, and replaces them with a <br> for the rich text实际上,它删除了隐藏的原始RichText格式。
在不改变内部RichText格式的情况下,更改文本内联颜色的最佳方法是这样做.
TextEdit1.Format.Color = Color.Red然后在那个位置输入的文本将是红色的。
这样您就可以监视Delete / Backspace,然后在当前位置将格式颜色设置为红色。
gambaswiki.org/wiki/comp/gb.qt4.ext/textedit
https://stackoverflow.com/questions/72578815
复制相似问题