QTextEditor 和 QPlainTextEditor 在一定程度上通用的

获取文本

1
toPlainText();

设置文本

1
setPlainText(QString);

获取行数

1
2
num = QString::number(plainTextEdit->document()->lineCount()); // 获取行数
str = plainTextEdit->document()->findBlockByLineNumber(2).text(); // 获取某一行

获取光标所在行文本

1
2
3
4
5
QTextCursor cursor = textEditor->textCursor();
int lineNumber = cursor.blockNumber();//获取光标所在列用cursor.columnNumber();

QTextBlock textBlock = QTextEdit::document()->findBlockByLineNumber(lineNumber);//通过行号找到指定行 数据块
QString selectLine = textBlock.text();//将得到的数据存入一个字符串。

内容改变信号

1
textChanged()

setText()时也会触发,如果要屏蔽,在setText()前调用QObject::blockSignals(true),setText()后调用QObject::blockSignals(false)

光标位置改变信号

1
cursorPositionChanged()

设置光标行列

1
2
3
4
5
6
7
8
9
10
11
12
void setCursorPos(int row,int col)
{
const QTextBlock block = QTextEdit::document()->findBlockByLineNumber(row-1);
if(block.isValid())
{
QTextCursor cursor = QTextEdit::textCursor();
cursor.setPosition(block.position()+col-1);
setTextCursor(cursor);
ensureCursorVisible();
}
setFocus();
}

光标在一行中的(字符?)位置

这里的nCursor其实就是我们一个文本在一行中的位置

1
2
QTextCursor tc = myTextEdit->textCursor();
int nCurpos = tc.position() - tc.block().position();

光标移动

1
2
3
QTextCursor textCursor= ui.translationInput->textCursor(); // 获取光标
/* 这里是针对光标的修改操作 */
ui.translationInput->setTextCursor(textCursor); // 修改光标后设置成编辑框光标

移动到行首

1
2
3
QTextCursor textCursor= ui.translationInput->textCursor();
textCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 4); // 行首
ui.translationInput->setTextCursor(textCursor);

移动到文末

1
textCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 4); // 全文尾

移动到指定字符

1
textCursor.setPosition(20);

右移一个单词

1
textCursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);

删除光标前一个字符

1
2
3
4
5
6
7
8
9
10
QTextEdit editer; // 这个是目标 Editor
QTextCursor cursor = editer.textCursor(); // 获取当前文本光标

//判断当前是否选中了文本,如果选中了文本则取消选中的文本,再删除前一个字符
if(cursor.hasSelection())
cursor.clearSelection();

cursor.deletePreviousChar(); //删除前一个字符

editer.setTextCursor(cursor); //设置当前的光标为更改后的光标

设置行高和行间距

1
2
3
4
5
6
7
8
QTextCursor textCursor = ui->textEdit->textCursor();

QTextBlockFormat textBlockFormat;
textBlockFormat.setLineHeight(40, QTextBlockFormat::FixedHeight); // 设置固定行高
textBlockFormat.setBottomMargin(10); // 设置两行之间的空白高度
textCursor.setBlockFormat(textBlockFormat);

ui->textEdit->setTextCursor(textCursor);

文字进行加粗操作

1
2
3
4
5
6
7
8
9
10
11
12
13
QTextCursor currentTextCursor = currentTextEdit->textCursor();

if (!currentTextCursor.hasSelection())
{
currentTextCursor.insertText("**" + tr("Boldface") + "**");
currentTextCursor.movePosition (QTextCursor::Left, QTextCursor::MoveAnchor, 2);
currentTextCursor.movePosition (QTextCursor::WordLeft, QTextCursor::KeepAnchor, 1);
currentTextEdit->setTextCursor (currentTextCursor);
}
else
{
currentTextCursor.insertText ("**" +currentTextCursor.selectedText() + "**");
}

高亮光标所在行

使用到qtextedit中的cursorPostionChanged(),这个信号将会在光标移动时实时发送。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
QTextEdit *edit = (QTextEdit *)tab->widget(tab->currentIndex());
connect(edit,SIGNAL(cursorPositionChanged()), this,SLOT(onCurrentLineHighLight()));

void MainWindow::onCurrentLineHighLight()
{
QTextEdit *edit = (QTextEdit *)tab->currentWidget();
QList<QTextEdit::ExtraSelection> extraSelection;
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::gray).lighter(150);
selection.format.setBackground(lineColor);
selection.format.setProperty(
QTextFormat::FullWidthSelection,true);
selection.cursor = edit->textCursor();
selection.cursor.clearSelection();
//将刚设置的 selection追加到链表当中
extraSelection.append(selection);
edit->setExtraSelections(extraSelection);
}

设置部分字体颜色

只能设置选中区域的字体颜色

1
2
3
4
5
6
7
8
9
QTextCursor textCursor = ui->plainTextEdit->textCursor();
if (textCursor.hasSelection())
{
int start = textCursor.selectionStart();
int end = textCursor.selectionEnd();
QTextCharFormat f;// = textCursor.charFormat(); // 返回前一个字的格式(没必要)
f.setForeground(QBrush(QColor(128,128,128, 20)));
textCursor.setCharFormat(f);
}