【Qt開發流程】之富文本處理

描述

Scribe框架提供了一組類,用于讀取和操作結構化的富文本文檔。與Qt中以前的富文本支持不同,新的類集中在QTextDocument類上,而不是原始文本信息。這使開發者能夠創建和修改結構化的富文本文檔,而不必準備中間標記格式的內容。

文檔中的信息可以通過兩個互補的接口訪問:基于光標的接口用于編輯只讀分層接口提供文檔結構的高層次概述。基于光標的接口的主要優點是,可以使用模擬用戶與編輯器交互的操作編輯文本,而不會丟失文檔的基礎結構。只讀分層接口在執行搜索和文檔導出等操作時最有用。

Qt中,富文本處理可以使用QTextEditQTextBrowser實現。這兩個類都繼承自QAbstractScrollArea類,提供了文本的顯示和編輯功能。

QTextEdit提供了類似于一個文本編輯器的界面,可以進行編輯和顯示富文本。需要注意的是,QTextEdit可以顯示圖片和嵌入式對象,但不能直接播放音頻和視頻。

QTextBrowser提供了一個只讀的文本顯示界面,可以用于顯示富文本內容。與QTextEdit不同,QTextBrowser只支持顯示圖片,不支持嵌入式對象。

Qt中實現富文本處理的流程:

  1. 創建QTextEditQTextBrowser對象,設置其屬性和初始文本內容;

  2. 使用QTextCursor操作文本內容,如插入文本、設置字體和顏色、插入圖片等;

  3. 將富文本內容保存到文件或使用QClipboard復制到剪貼板中;

  4. 在需要的位置顯示富文本內容,如在某個對話框中、在網頁中等。

示例代碼實現在QTextEdit中插入一張圖片和設置不同的字體和顏色:
注意:在使用插入圖片功能時,需要將圖片資源添加到Qt項目中,并使用相應的路徑引用。

    ui->textEdit->setPlainText("Hello World!");  //設置初始文本內容ui->textEdit->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard); //設置文本可選中屬性為falseQTextCursor cursor = ui->textEdit->textCursor(); //獲取文本光標cursor.insertImage(":/images/image.png"); //插入圖片QFont font("Arial", 16, QFont::Bold); //設置字體cursor.movePosition(QTextCursor::End); //將光標移動到文本結尾QTextCharFormat format;format.setFont(font);cursor.insertText("\nQt is great!", format); //插入文本并設置字體QColor color(Qt::red); //設置顏色cursor.movePosition(QTextCursor::Start); //將光標移動到文本開頭cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 5); //選中第一個單詞
//    cursor.setCharFormat(QTextCharFormat()); //清除之前的格式
//    cursor.setCharFormat(QTextCharFormat(), QTextCharFormat::ForegroundBrush); //設置前景色ui->textEdit->setTextCursor(cursor);

在這里插入圖片描述

富文本相關類

Qt提供了大量的類來解析、渲染、操作和編輯富文本。

模塊描述解釋
QAbstractTextDocumentLayoutAbstract base class used to implement custom layouts for QTextDocuments用于為QTextDocuments實現自定義布局的抽象基類
QFontSpecifies a font used for drawing text指定用于繪制文本的字體
QSyntaxHighlighterAllows you to define syntax highlighting rules, and in addition you can use the class to query a document’s current formatting or user data允許定義語法高亮規則,并且還可以使用該類查詢文檔的當前格式或用戶數據
QTextCursorOffers an API to access and modify QTextDocuments提供訪問和修改QTextDocuments的API
QTextDocumentHolds formatted text維護格式化的文本
QTextDocumentFragmentRepresents a piece of formatted text from a QTextDocument表示來自QTextDocument的格式化文本片段
QTextDocumentWriterFormat-independent interface for writing a QTextDocument to files or other devices用于將QTextDocument寫入文件或其他設備的格式無關接口
QTextBlockFormatFormatting information for blocks of text in a QTextDocumentQTextDocument中文本塊的格式信息
QTextCharFormatFormatting information for characters in a QTextDocumentQTextDocument中字符的格式信息
QTextFormatFormatting information for a QTextDocumentQTextDocument的格式信息
QTextFrameFormatFormatting information for frames in a QTextDocumentQTextDocument中框架的格式信息
QTextImageFormatFormatting information for images in a QTextDocumentQTextDocument中圖像的格式信息
QTextLengthEncapsulates the different types of length used in a QTextDocument封裝在QTextDocument中使用的不同類型的長度
QTextListFormatFormatting information for lists in a QTextDocumentQTextDocument中列表的格式信息
QTextTableCellFormatFormatting information for table cells in a QTextDocumentQTextDocument中表單元格的格式信息
QTextTableFormatFormatting information for tables in a QTextDocumentQTextDocument中表格的格式信息
QTextInlineObjectRepresents an inline object in a QAbstractTextDocumentLayout and its implementations表示QAbstractTextDocumentLayout及其實現中的內聯對象
QTextLayoutUsed to lay out and render text用于布局和呈現文本
QTextLineRepresents a line of text inside a QTextLayout表示QTextLayout內的一行文本
QTextListDecorated list of items in a QTextDocumentQTextDocument中裝飾了的列表項
QTextBlockContainer for text fragments in a QTextDocumentQTextDocument中文本片段的容器
QTextBlockGroupContainer for text blocks within a QTextDocumentQTextDocument中文本塊的容器
QTextBlockUserDataUsed to associate custom data with blocks of text用于將自定義數據與文本塊關聯
QTextFragmentHolds a piece of text in a QTextDocument with a single QTextCharFormat在QTextDocument中保留具有單個QTextCharFormat的文本片段
QTextFrameRepresents a frame in a QTextDocument表示QTextDocument中的框架
QTextObjectBase class for different kinds of objects that can group parts of a QTextDocument together不同類型的對象的基類,這些對象可以將QTextDocument的部分組合在一起
QTextFrame::iteratorIterator for reading the contents of a QTextFrame用于讀取QTextFrame內容的迭代器
QTextBlock::iteratorQTextBlock::iterator class provides an iterator for reading the contents of a QTextBlockQTextBlock::iterator類為讀取QTextBlock內容提供迭代器
QTextOptionDescription of general rich text properties通用富文本屬性的描述
QTextTableRepresents a table in a QTextDocument表示QTextDocument中的表格
QTextTableCellRepresents the properties of a cell in a QTextTable表示QTextTable中單元格的屬性
QPlainTextDocumentLayoutImplements a plain text layout for QTextDocument為QTextDocument實現純文本布局
QPlainTextEditWidget that is used to edit and display plain text用于編輯和顯示純文本的小部件
QTextBrowserRich text browser with hypertext navigation富文本瀏覽器,帶有超文本導航
QTextEditWidget that is used to edit and display both plain and rich text用于編輯和顯示純文本和富文本的小部件

QTextEdit的cursor和document關系圖

在這里插入圖片描述

Documen框架圖

在這里插入圖片描述

QTextFrame

    QTextDocument* doc = ui->textEdit->document();QTextFrame* frame = doc->rootFrame();QTextFrameFormat format;format.setBorderBrush(Qt::red);format.setBorder(3);frame->setFrameFormat(format);

運行程序,發現只可以在紅色邊框內編輯。
在這里插入圖片描述
使用光標類對象,在根框架中添加一個子框架。

    QTextFrameFormat frameFormat;frameFormat.setBackground(Qt::lightGray);frameFormat.setMargin(10);frameFormat.setPadding(10);frameFormat.setBorder(2);frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted);QTextCursor cursor = ui->textEdit->textCursor();cursor.insertFrame(frameFormat);

顯示如下:
在這里插入圖片描述

文本塊

綁定信號和槽,輸出文本塊,以下只可獲取文本塊,不可獲取frame的內容。

void MainWindow::on_action_frame_triggered()
{QTextDocument* doc = ui->textEdit->document();QTextFrame* frame = doc->rootFrame();QTextFrame::iterator it = frame->begin();while (!it.atEnd()) {QTextFrame* childFrame = it.currentFrame();QTextBlock childBlock = it.currentBlock();if(childFrame){qDebug() << "frame";}else if(childBlock.isValid()){qDebug() << "block :" << childBlock.text();}++it;}
}

顯示如下:
在這里插入圖片描述
在這里插入圖片描述
獲取frame的內容:

void MainWindow::on_actionframeText_triggered()
{QTextDocument* doc = ui->textEdit->document();QTextBlock block = doc->firstBlock();for (int var = 0; var < doc->blockCount(); ++var) {qDebug() << block.text();block = block.next();}}

界面及結果:
在這里插入圖片描述
在這里插入圖片描述

QTextBlockFormat和QTextCharFormat

QTextBlockFormat:設置對齊方式、縮進等格式;
QTextCharFormat:設置字體、顏色、下劃線等格式。

    QTextBlockFormat blockFormat;blockFormat.setAlignment(Qt::AlignCenter);cursor.insertBlock(blockFormat);QTextCharFormat charFormat;charFormat.setBackground(Qt::lightGray);charFormat.setForeground(Qt::blue);charFormat.setFont(QFont("宋體", 12, QFont::Bold, true));charFormat.setFontUnderline(true);cursor.setCharFormat(charFormat);cursor.insertText("helloworld");

效果如下:
在這里插入圖片描述

插入表格、列表、圖片

表格相關類:

QTextTableFormatQTextTableCellFormat QTextTable

    QTextTable *insertTable(int rows, int cols, const QTextTableFormat &format);QTextTable *insertTable(int rows, int cols);QTextTable *currentTable() const;

示例:

    QTextTableFormat tableFormat;tableFormat.setCellSpacing(2);tableFormat.setCellPadding(10);cursor.insertTable(2, 2, tableFormat);

在這里插入圖片描述

列表相關類:

QTextListFormatQTextList

    QTextList *insertList(const QTextListFormat &format);QTextList *insertList(QTextListFormat::Style style);QTextList *createList(const QTextListFormat &format);QTextList *createList(QTextListFormat::Style style);QTextList *currentList() const;

示例:

    QTextListFormat listFormat;listFormat.setStyle(QTextListFormat::ListDecimal);cursor.insertList(listFormat);
圖片相關類:

QTextImageFormat

    void insertImage(const QTextImageFormat &format, QTextFrameFormat::Position alignment);void insertImage(const QTextImageFormat &format);void insertImage(const QString &name);void insertImage(const QImage &image, const QString &name = QString());

查找

查找到后,可以定位字符串所在的行和列的編號。

qDebug() << ui->textEdit->find("hello world", QTextDocument::FindBackward);ui->textEdit->textCursor().blockNumber();
ui->textEdit->textCursor().columnNumber();

在這里插入圖片描述
在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/160486.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/160486.shtml
英文地址,請注明出處:http://en.pswp.cn/news/160486.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【數據結構】A : A DS圖_傳遞信息

A : A DS圖_傳遞信息 Description 小明在和他的小伙伴們玩傳消息游戲&#xff0c;游戲規則如下&#xff1a; 有n名玩家&#xff0c;所有玩家編號分別為0~n-1&#xff0c;其中小明編號為0&#xff1b;每個玩家都有固定的若干個可傳信息的其他玩家(也可能沒有)。傳消息的關系是…

busybox制作根文件系統2

上篇內容使用busybox制作好了根文件系統&#xff0c;接下來需要進行一些測試和功能的完善&#xff01; 根文件系統的測試 測試根文件系統的時候不是直接燒寫到EMMC里面&#xff0c;這樣測試效率太低了&#xff0c;Ubuntu的rootfs目錄已經保存了根文件系統&#xff0c;只需要在…

向量數據庫,展望AGI時代

無論是向量數據庫&#xff0c;還是大模型&#xff0c;歸根結底&#xff0c;大家在追捧它時的心態&#xff0c;焦慮大于需求。 向量數據庫的熱潮&#xff0c;在一定程度上“外化”了人們的焦慮。 但這并不能否定向量數據庫的實際價值&#xff0c;甚至更長遠來看&#xff0c;向…

【C++】linux下的gdb程序調試

目錄 【C】Linux 下的 GDB 程序調試1. 安裝 GDB2. 編譯程序3. 啟動 GDB4. 設置斷點5. 執行程序6. 調試命令7. 調試崩潰8. 結束調試 【C】Linux 下的 GDB 程序調試 在開發 C 程序時&#xff0c;出現 bug 是常見的。調試是找出程序錯誤的關鍵步驟之一。在 Linux 環境下&#xff…

RedisTemplate使用詳解

RedisTemplate介紹StringRedisTemplate介紹RedisConnectionFactory介紹RedisConnectionFactory源碼解析 RedisOperations介紹RedisOperations源碼解析 RedisTemplate使用連接池配置RedisTemplate連接池連接池配置 RedisTemplate應用場景RedisTemplate主要特點RedisTemplate使用…

redis運維(十六) 有序集合

一 有序集合 把握一點&#xff1a; 各種redis 命令都提供各種語言對應的API 接口,后續API是關鍵 ① 概念 1、sorted set --> 有序集合2、redis有序集合也是集合類型的一部分&#xff0c;所以它保留了集合中元素不能重復的特性3、但是不同的是,有序集合給每個元素多設置…

什么是數字孿生?

數字孿生是指通過數字化技術手段&#xff0c;將現實世界中的實體物理系統或過程與其數字化模型相連接&#xff0c;實現實體物理系統或過程的虛擬仿真、監測、預測和優化等功能的一種技術。數字孿生技術可以將物理系統的運行狀態、性能參數、故障信息等實時反饋到數字模型中&…

轉型做視頻了,博客就是稿子,繼續堅持寫博客,同時發布視頻,能寫博客說明思路清晰了,能再講明白,理解就更透徹了,緊跟上時代發展。

1&#xff0c;今天特別記錄下&#xff0c;B站給開通了《合集》功能 最近使用視頻制作了幾個視頻。播放量還不錯&#xff0c;最好的已經到了 2.6K了。 然后粉絲也漲到了 200個。 添加鏈接描述 緊跟時代&#xff1a;從寫博客到錄視頻&#xff0c;粉絲大漲&#xff0c;突破200個&…

vue開發一、在Vue中引入ElementUI二、在Vue中使用阿里圖標庫

目錄 一、在Vue中引入ElementUI1. 安裝ElementUI2. 引入ElementUI3. 使用ElementUI組件 二、在Vue中使用阿里圖標庫1. 在阿里圖標庫中選擇圖標2. 下載圖標3. 引入圖標4. 使用圖標 總結 一、在Vue中引入ElementUI ElementUI是一種基于Vue的第三方UI庫&#xff0c;提供了許多常用…

接口自動化測試 —— 工具、請求與響應

一、工具&#xff1a; 1.工具介紹 postman &#xff1a;很主流的API測試工具&#xff0c;也是工作里面使用最廣泛的研發工具。 JMeter&#xff1a; ApiPost&#xff1a; 2.安裝postman&#xff1a; 安裝好直接打開&#xff0c;不用注冊。 二、通信模式&#xff1a; 1、…

【Java 進階篇】從Java對象到JSON:Jackson的魔法之旅

在現代的軟件開發中&#xff0c;處理數據的能力是至關重要的。而當我們談及數據格式時&#xff0c;JSON&#xff08;JavaScript Object Notation&#xff09;通常是首選。為了在Java中輕松地將對象轉換為JSON&#xff0c;我們需要一種強大而靈活的工具。這時&#xff0c;Jackso…

【Java 進階篇】Redis:打開緩存之門

介紹 Redis&#xff08;Remote Dictionary Server&#xff09;是一個高性能的鍵值對存儲系統&#xff0c;被廣泛用作緩存、消息中間件和數據庫。它以其快速的讀寫能力、支持多種數據結構和豐富的功能而聞名。在這篇博客中&#xff0c;我們將深入了解Redis的概念、安裝以及基本…

MQTT協議消息代理服務遠程連接

目錄 1. Linux 搭建 Mosquitto 2. Linux 安裝Cpolar 3. 創建MQTT服務公網連接地址 4. 客戶端遠程連接MQTT服務 5. 代碼調用MQTT服務 6. 固定連接TCP公網地址 7. 固定地址連接測試 Mosquitto是一個開源的消息代理&#xff0c;它實現了MQTT協議版本3.1和3.1.1。它可以在不…

第二十章:多線程

進程 線程的特點 1.進程是資源分配的最小單位&#xff0c;線程是最小的執行單位 2.一個進程可以有多個線程 3.線程共享進程資源 package twentyth; public class ThreadTest extends Thread { public void run() { for (int i 1; i < 10; i) {//繼承重…

Unity開發之C#基礎-File文件讀取

前言 今天我們將要講解到c#中 對于文件的讀寫是怎樣的 那么沒接觸過特別系統編程小伙伴們應該會有一個疑問 這跟文件有什么關系呢&#xff1f; 我們這樣來理解 首先 大家對電腦或多或少都應該有不少的了解吧 那么我們這些軟件 都是通過變成一個一個文件保存在電腦中 我們才可以…

【2023C卷最新題目】20天拿下華為OD筆試之【貪心】2023C-找座位/2023B-座位調整-全網注釋最詳細分類最全的華為OD真題題解

文章目錄 題目描述與示例題目描述輸入輸出說明示例一輸入輸出 示例二輸入輸出說明 解題思路代碼PythonJavaC時空復雜度 相同問題不同描述2023C-找座位題目描述輸入描述輸出描述示例一輸入輸出 示例二輸入輸出 華為OD算法/大廠面試高頻題算法練習沖刺訓練 題目描述與示例 題目描…

Spring Boot創建和使用(重要)

Spring的誕生是為了簡化Java程序開發的&#xff01; Spring Boot的誕生是為了簡化Spring程序開發的&#xff01; Spring Boot就是Spring框架的腳手架&#xff0c;為了快速開發Spring框架而誕生的&#xff01;&#xff01; Spring Boot的優點&#xff1a; 快速集成框架&#x…

2023年G2電站鍋爐司爐證考試題庫及G2電站鍋爐司爐試題解析

題庫來源&#xff1a;安全生產模擬考試一點通公眾號小程序 2023年G2電站鍋爐司爐證考試題庫及G2電站鍋爐司爐試題解析是安全生產模擬考試一點通結合&#xff08;安監局&#xff09;特種作業人員操作證考試大綱和&#xff08;質檢局&#xff09;特種設備作業人員上崗證考試大綱…

MySQL 事務的底層原理和 MVCC(一)

在事務的實現機制上&#xff0c;MySQL 采用的是 WAL&#xff08;Write-ahead logging&#xff0c;預寫式日志&#xff09;機制來實現的。 在使用 WAL 的系統中&#xff0c;所有的修改都先被寫入到日志中&#xff0c;然后再被應用到系統中。通常包含 redo 和 undo 兩部分信息。 …

【Java開發】 Springboot集成Mybatis-Flex

1 Mybatis-Flex 介紹 1.1簡介 Mybatis-Flex 是一個優雅的 Mybatis 增強框架&#xff0c;它非常輕量、同時擁有極高的性能與靈活性。我們可以輕松的使用 Mybaits-Flex 鏈接任何數據庫&#xff0c;其內置的 QueryWrapper 亮點幫助我們極大的減少了 SQL 編寫的工作的同時&#xff…