保存qtextedit的光标位置
save cursor position of qtextedit
setCurrentCharFormat() 函数不将当前光标位置作为参数。因此,为了在控件中为任意文本设置 char 格式,我必须保存当前光标位置,设置 char 格式,然后恢复它。
但是,我在文档中没有看到任何类似 cursorPosition() 的东西。
我是不是漏掉了什么?
或者也许有更好的方法来做我想做的事情?
我认为您正在寻找 QTextEdit::textCursor()
method which returns a copy of the editor's QTextCursor
. You can then manipulate the cursor as needed (including changing char. format and inserting text with specific format)。如果您需要光标更改保持不变(如 char. 格式),请确保之后 QTextEdit::setCursor()
。
插入一些文本的一个非常基本的例子:
QTextCursor cursor(ui->textEdit->textCursor());
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor, 1);
cursor.insertText(text);
添加:
也许我们在 Qt Text Edit example will help. Specifically, in textedit.cpp 中看到这样的东西:
void TextEdit::textBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextCursor cursor = textEdit->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
textEdit->mergeCurrentCharFormat(format);
}
第二次添加(基于评论):
So, if my cursor is at the line 3 col 10 and I call the function like this: SetCharFormat( 2, 5, attr ); it stores the current cursor position as (3, 10), then selects the text for text characters 2 and 5, set the text attribute for the selection, and then cursor will move back to the old position/selection.
下面是我认为您所描述内容的具体示例。不过总的来说,文本光标位置只有一维,本质上是从文档开头算起的可见字符数。 (文本光标不应与 QCursor
混淆,后者表示具有 x,y 坐标的鼠标指针。)
这个简单的测试显示了一个带有文本 "I like this program." 和一个按钮的编辑器。该按钮(或 Alt-D
)将切换单词 "like" 的粗体格式,同时保持可见光标位置(和任何选择)不变。
我还提供了一些最初移动可见光标的示例代码,在格式化函数中有一个 commented-out 示例,说明如何以编程方式保存和恢复光标位置。在此特定示例中不需要它,因为从未修改可见光标。
#include <QtWidgets>
class Dialog : public QDialog
{
public:
Dialog(QWidget *parent = nullptr) : QDialog(parent)
{
QTextEdit *textEdit = new QTextEdit("I like this program.", this);
// Position cursor at end of sentence (just as an example)
QTextCursor cursor(textEdit->textCursor());
cursor.movePosition(QTextCursor::End);
textEdit->setTextCursor(cursor); // required for the visible cursor to actually move
QToolButton *btnTest = new QToolButton(this);
btnTest->setText("&Do it");
btnTest->setCheckable(true);
connect(btnTest, &QToolButton::toggled, this, [textEdit, btnTest](bool checked)
{
// Function to toggle bold formatting on a section of text in the editor.
const int start = 2; // start of "like"
const int end = start + 4; // length of "like"
// the formatting to be applied
QTextCharFormat format;
format.setFontWeight(checked ? QFont::Bold : QFont::Normal);
format.setForeground(checked ? QBrush(Qt::red) : QPalette().text());
format.setBackground(checked ? QBrush(Qt::gray) : QPalette().base());
QTextCursor cursor(textEdit->textCursor()); // get a copy of the editor's cursor
// const int oldCursorPos = cursor.position(); // save cursor position (not needed for this example)
cursor.setPosition(start, QTextCursor::MoveAnchor); // move w/out selection
cursor.setPosition(end, QTextCursor::KeepAnchor); // move and select
cursor.mergeCharFormat(format); // apply format to selection
// cursor.setCharFormat(format); // alternative to mergeChatFormat()
// cursor.setPosition(oldCursorPos); // restore cursor position
// cursor.setPosition(end); // or move it to the end of the affected text
// textEdit->setTextCursor(cursor); // required for the visible cursor to move
btnTest->setText(checked ? "Un&do it" : "Re&do it");
});
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(textEdit);
layout->addWidget(btnTest);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return Dialog().exec();
}
setCurrentCharFormat() 函数不将当前光标位置作为参数。因此,为了在控件中为任意文本设置 char 格式,我必须保存当前光标位置,设置 char 格式,然后恢复它。
但是,我在文档中没有看到任何类似 cursorPosition() 的东西。
我是不是漏掉了什么?
或者也许有更好的方法来做我想做的事情?
我认为您正在寻找 QTextEdit::textCursor()
method which returns a copy of the editor's QTextCursor
. You can then manipulate the cursor as needed (including changing char. format and inserting text with specific format)。如果您需要光标更改保持不变(如 char. 格式),请确保之后 QTextEdit::setCursor()
。
插入一些文本的一个非常基本的例子:
QTextCursor cursor(ui->textEdit->textCursor());
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor, 1);
cursor.insertText(text);
添加: 也许我们在 Qt Text Edit example will help. Specifically, in textedit.cpp 中看到这样的东西:
void TextEdit::textBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextCursor cursor = textEdit->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
textEdit->mergeCurrentCharFormat(format);
}
第二次添加(基于评论):
So, if my cursor is at the line 3 col 10 and I call the function like this: SetCharFormat( 2, 5, attr ); it stores the current cursor position as (3, 10), then selects the text for text characters 2 and 5, set the text attribute for the selection, and then cursor will move back to the old position/selection.
下面是我认为您所描述内容的具体示例。不过总的来说,文本光标位置只有一维,本质上是从文档开头算起的可见字符数。 (文本光标不应与 QCursor
混淆,后者表示具有 x,y 坐标的鼠标指针。)
这个简单的测试显示了一个带有文本 "I like this program." 和一个按钮的编辑器。该按钮(或 Alt-D
)将切换单词 "like" 的粗体格式,同时保持可见光标位置(和任何选择)不变。
我还提供了一些最初移动可见光标的示例代码,在格式化函数中有一个 commented-out 示例,说明如何以编程方式保存和恢复光标位置。在此特定示例中不需要它,因为从未修改可见光标。
#include <QtWidgets>
class Dialog : public QDialog
{
public:
Dialog(QWidget *parent = nullptr) : QDialog(parent)
{
QTextEdit *textEdit = new QTextEdit("I like this program.", this);
// Position cursor at end of sentence (just as an example)
QTextCursor cursor(textEdit->textCursor());
cursor.movePosition(QTextCursor::End);
textEdit->setTextCursor(cursor); // required for the visible cursor to actually move
QToolButton *btnTest = new QToolButton(this);
btnTest->setText("&Do it");
btnTest->setCheckable(true);
connect(btnTest, &QToolButton::toggled, this, [textEdit, btnTest](bool checked)
{
// Function to toggle bold formatting on a section of text in the editor.
const int start = 2; // start of "like"
const int end = start + 4; // length of "like"
// the formatting to be applied
QTextCharFormat format;
format.setFontWeight(checked ? QFont::Bold : QFont::Normal);
format.setForeground(checked ? QBrush(Qt::red) : QPalette().text());
format.setBackground(checked ? QBrush(Qt::gray) : QPalette().base());
QTextCursor cursor(textEdit->textCursor()); // get a copy of the editor's cursor
// const int oldCursorPos = cursor.position(); // save cursor position (not needed for this example)
cursor.setPosition(start, QTextCursor::MoveAnchor); // move w/out selection
cursor.setPosition(end, QTextCursor::KeepAnchor); // move and select
cursor.mergeCharFormat(format); // apply format to selection
// cursor.setCharFormat(format); // alternative to mergeChatFormat()
// cursor.setPosition(oldCursorPos); // restore cursor position
// cursor.setPosition(end); // or move it to the end of the affected text
// textEdit->setTextCursor(cursor); // required for the visible cursor to move
btnTest->setText(checked ? "Un&do it" : "Re&do it");
});
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(textEdit);
layout->addWidget(btnTest);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return Dialog().exec();
}