1

I want to put the cursor at the start of a line in a QPlainTextEdit specified by its line number. The problem is I have the "real" line number, i.e. the number of \n between the line and the start, while the cursor seems to be using "virtual" line numbers, i.e. including the line wraps done by the textedit.

Which means that the following does not work correctly:

old_line = textedit.textCursor().blockNumber()
line = token.line
diff = line - old_line
move = QtGui.QTextCursor.Down if diff >= 0 else QtGui.QTextCursor.Up
for _ in range(abs(diff)): textedit.moveCursor(move, QtGui.QTextCursor.MoveAnchor)
4

1 回答 1

0

The simple solution I went with was to deactivate line wrapping before moving the cursor and activating it afterwards again:

textedit.setLineWrapMode(QtGui.QPlainTextEdit.NoWrap)
# above code in between here
textedit.setLineWrapMode(QtGui.QPlainTextEdit.WidgetWidth)

Most certainly horribly inefficient but for my few hundred lines long text it works fine without any noticeable lag.

于 2012-05-14T15:53:47.927 回答