QTextEditor 和 QPlainTextEditor 在一定程度上通用的
获取文本
设置文本
获取行数
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();
QTextBlock textBlock = QTextEdit::document()->findBlockByLineNumber(lineNumber); QString selectLine = textBlock.text();
|
内容改变信号
在setText()
时也会触发,如果要屏蔽,在setText()前调用QObject::blockSignals(true)
,setText()后调用QObject::blockSignals(false)
光标位置改变信号
设置光标行列
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; 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(); 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; f.setForeground(QBrush(QColor(128,128,128, 20))); textCursor.setCharFormat(f); }
|