介紹QTextEdit簡單使用,包括插入圖片和表格。并表格單元格設(shè)置、內(nèi)容選擇、刪除。
表格
- 插入表格
auto *table = ui->textEdit->textCursor().insertTable(2, 1, tableFormat);
- 設(shè)置表格格式
QTextTableFormat tableFormat;
tableFormat.setBorderCollapse(true);
tableFormat.setCellSpacing(0);
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_None);
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
setBorderCollapse設(shè)置是否合并相鄰單元格的邊框。
setCellSpacing設(shè)置單元格周圍的空間。
[圖片上傳失敗...(image-e82064-1664444181948)]
setWidth可以按照絕對值或者百分比設(shè)置表格的寬度。100%表示表格橫向填滿文檔。
-
設(shè)置單元格
QTextBlockFormat blockFormat; blockFormat.setAlignment(Qt::AlignCenter); QTextTableCell cell = table->cellAt(0, 0); QTextTableCellFormat cellFormat; cellFormat.setFontPointSize(15); cell.setFormat(cellFormat); cell.firstCursorPosition().setBlockFormat(blockFormat); cell.firstCursorPosition().insertText(ui->leTitle->text().trimmed());QTextTableCellFormat設(shè)置每個單元格的格式,比如單元格的邊框?qū)挾?、顏色以及邊距?br>QTextBlockFormat設(shè)置單元格的文本格式,比如居中對齊。
橫線
可以通過Frame或者Table的方式插入橫線。
QTextFrameFormat frameFormat;
frameFormat.setHeight(1);
frameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
frameFormat.setBackground(Qt::darkGray);
ui->textEdit->textCursor().insertFrame(frameFormat);
ui->textEdit->moveCursor(QTextCursor::MoveOperation::NextBlock);
注意:ui->textEdit->moveCursor(QTextCursor::MoveOperation::NextBlock);用于移動到下一個段落,不然后續(xù)操作還在當前Frame內(nèi)。
使用表格時,插入1*1的表格,將上邊框或者下邊框設(shè)為1即可。
圖片
可以直接插入QImage,不過最好的辦法是使用資源,可以控制圖片細節(jié)。
ui->textEdit->document()->addResource(QTextDocument::ImageResource, QUrl("canvas"), canvas);
QTextImageFormat imageFormat;
imageFormat.setName("canvas");
imageFormat.setQuality(100);
imageFormat.setWidth(contentRect.width());
ui->textEdit->textCursor().insertImage(imageFormat);
內(nèi)容選擇及刪除
比如選擇單元格內(nèi)容。獲取單元格的光標起始位置,然后保持錨點不變,移動到單元格末尾。
auto cursor = table->cellAt(0, 0).firstCursorPosition();
int delta = table->cellAt(0, 0).lastPosition() - table->cellAt(0, 0).firstPosition();
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, delta); // 保持錨點,并把光標移到末尾
cursor.removeSelectedText(); // 刪除錨點到末尾內(nèi)容
cursor.insertText("hello");
獲取選擇部分文本
同樣是先選擇內(nèi)容,然后可以通過selectedText、selection獲取選擇部分。需要注意的是,selectedText獲取純文本,不包含富文本內(nèi)容,另外換行符會被編碼為U+2029。
QTextCursor Class | Qt GUI 6.3.2
QString QTextCursor::selectedText() const
Returns the current selection's text (which may be empty). This only returns the text, with no rich text formatting information. If you want a document fragment (i.e. formatted rich text) use selection() instead.
Note: If the selection obtained from an editor spans a line break, the text will contain a Unicode U+2029 paragraph separator character instead of a newline \n character. Use QString::replace() to replace these characters with newlines.
QTextDocumentFragment QTextCursor::selection() const
Returns the current selection (which may be empty) with all its formatting information. If you just want the selected text (i.e. plain text) use selectedText() instead.
Note: Unlike QTextDocumentFragment::toPlainText(), selectedText() may include special unicode characters such as QChar::ParagraphSeparator.
auto cursor = diagnosisTable->cellAt(0, 0).firstCursorPosition();
int delta = diagnosisTable->cellAt(0, 0).lastPosition() - diagnosisTable->cellAt(0, 0).firstPosition();
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, delta); // 保持錨點,并把光標移到末尾
qDebug() << cursor.selectedText().replace(QChar::ParagraphSeparator, '\n');
qDebug() << cursor.selection().toPlainText();
版權(quán)聲明:本文為「txfly」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:http://m.itdecent.cn/p/2255fc0b9484