保存qtextedit的光标位置

时间:2019-06-24 20:33:26

标签: qt qtextedit

setCurrentCharFormat()函数不将当前光标位置作为参数。因此,为了在控件中为任意文本设置char格式,我必须保存当前光标位置,设置char格式,然后将其恢复。

但是,我在文档中看不到任何类似cursorPosition()的东西。

我想念什么吗?

或者也许有一种更好的方式来做我想做的事?

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找一种QTextEdit::textCursor()方法,该方法返回编辑者QTextCursor副本。然后,您可以根据需要操作光标(包括更改字符格式和使用specific format插入文本)。如果您需要保留光标更改(例如字符格式),请确保之后再QTextEdit::setCursor()

一个非常基本的示例,用于插入一些文本:

  QTextCursor cursor(ui->textEdit->textCursor());
  cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor, 1);
  cursor.insertText(text);


添加: Qt Text Edit example可能会有所帮助。具体来说,在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);
  }


第二添加项(基于评论):

  

因此,如果我的光标位于第3行第10行,并且我这样调用该函数:SetCharFormat(2,5,attr);它将当前光标位置存储为(3,10),然后选择文本字符2和5的文本,设置选择的文本属性,然后光标将移回原来的位置/选择。

以下是我认为您正在描述的特定示例。总的来说,文本光标的位置只有一维,从本质上讲,这是从文档开始处可见字符的数量。 (文本光标不应与QCursor混淆,Alt-D代表具有x,y坐标的鼠标指针。)

这个简单的测试向编辑器显示了文本“我喜欢这个程序”。和一个按钮。该按钮(或#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(); } )将在“赞”一词上切换粗体格式,同时保持可见光标位置(和任何选择)不变。

我还提供了一些示例代码,这些示例代码最初会移动可见光标,并且在格式化功能中,有一个注释掉的示例,该示例说明了如何以编程方式保存和恢复光标位置。在此特定示例中不需要,因为从不修改可见光标。

{{1}}
相关问题