QTextEdit使用

介紹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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)自鏈接 目錄 1.認識NPOI 2.使用NPOI生成xls文件 2.1創(chuàng)建基本內(nèi)容 2.1.1創(chuàng)建Workboo...
    腿毛褲閱讀 11,165評論 1 3
  • 使用首先需要了解他的工作原理 1.POI結(jié)構(gòu)與常用類 (1)創(chuàng)建Workbook和Sheet (2)創(chuàng)建單元格 (...
    長城ol閱讀 8,755評論 2 25
  • VBA訂制工具欄 http://club.excelhome.net/thread-1047254-1-1.htm...
    大海一滴寫字的地方閱讀 2,359評論 0 0
  • swift/OSX 原文:http://www.raywenderlich.com/87002/getting-s...
    鴻楓亂閱讀 6,499評論 2 4
  • 轉(zhuǎn)自鏈接 2.3.5 IF函數(shù) 2.3.6 CountIf和SumIf函數(shù) 2.3.7 Lookup函數(shù) 2.3....
    腿毛褲閱讀 13,235評論 0 0

友情鏈接更多精彩內(nèi)容