gpt4 book ai didi

c++ - 保留 QTextEdit 中的光标位置

转载 作者:行者123 更新时间:2023-11-30 01:21:59 25 4
gpt4 key购买 nike

我有一个 QTextEdit 控件。它有一个最大限制(它可以容纳的最大字符数)。为实现这一点,我将一个插槽连接到 textChanged() 信号,当字符总数超过允许的最大值时,该信号会删除多余的字符。

有了这个,我在处理光标位置时遇到了一些问题。谁能告诉我如何在 QTextEdit 中保留光标位置?

最佳答案

在你的位置上:

如果字符数超过最大值:

Ask the QTextEdit for the Cursor:

QTextCursor QTextEdit::textCursor() const

Set the return value as your textEdit cursor (cause it returns a copy). From doc:

Returns a copy of the QTextCursor that represents the currently visible cursor. Note that > changes on the returned cursor do not affect QTextEdit's cursor; use setTextCursor() to > update the visible cursor.

void QTextEdit::setTextCursor(const QTextCursor & cursor)

Ask the cursor to delete last char

void QTextCursor::deletePreviousChar()

(编辑)作为代码:

QTextCursor  cursor = ui->textEdit->textCursor();
ui->textEdit->setTextCursor( cursor );
cursor.deletePreviousChar();

关于c++ - 保留 QTextEdit 中的光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17182931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com