QT 编写Rtf(富文本格式) 文件实例
生活随笔
收集整理的這篇文章主要介紹了
QT 编写Rtf(富文本格式) 文件实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
QT 編寫Rtf(富文本格式) 文件實例
2016-01-10 14:11:25?小K小Q?閱讀數 3450更多
分類專欄:?QT學習之路
版權聲明:本文為博主原創文章,遵循?CC 4.0 BY-SA?版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/li235456789/article/details/50492078
首先, 什么是rtf 格式, rtf稱為富文本格,具體的內容如下 :
?
rtf格式是又叫富文本格式或者多文本格式,RTF是Rich Text Format的縮寫,意即豐富的文本格式。在rtf文檔中可以嵌入圖像等文件,RTF是word為了與其他字處理軟件兼容而能夠保存的文檔格式,類似 DOC格式(Word文檔)的文件,有很好的兼容性。 用什么軟件可以打開?使用Windows“附件”中的“寫字板”就能打開并進行編輯。 使用“寫字板”打開一個RTF格式文件時,將看到文件的內容;如果要查看RTF格式文件的源代碼,只要使用“記事本”將它打開就行了。 這就是說,你完全可以像編輯HTML文件一樣,使用“記事本”來編輯RTF格式文件。 許多軟件都能夠識別rtf文件格式。比如Word2003、WPS Office、Excel等都可以打開RTF格式的文件,例如如將WPS文件另存為RTF格式,用Word進行編輯處理,原WPS下設置的字形、字號保持不變,這說明這種格式是較為通用的。 rft格式的優點和缺點: 對普通用戶而言,RTF格式是一個很好的文件格式轉換工具,用于在不同應用程序之間進行格式化文本文檔的傳送。 下面講一下實例內容 1. 需要添加有的模塊。 在.pro 文件里面加入 QT += xml 2.rft文件生成需要的頭文件為 #include <QtXml/QDomDocument> 本實例還需要用到, ? #include <QFileDialog> // 用戶自定義保存路徑 #include <QTextStream> // 文件流保存所需內容 3.實例代碼: ? #include "widget.h" #include "ui_widget.h" #include <QFileDialog> #include <QtXml/QDomDocument> #include <QTextStream> ? Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); } ? Widget::~Widget() { delete ui; } ? void Widget::on_toolButton_clicked() { QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(), tr("Rich Text Files (*.rtf);;")); if (fileName.isEmpty()) return; if (!fileName.endsWith(QLatin1String(".rtf"))) fileName += QLatin1String(".rtf"); ? // 上面那塊代碼,主要用于用戶手動選擇保存路徑, 以及輸入文件名后, 如果用戶沒有加上后綴,自動加上文件后綴。 ? QFile file(fileName); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) { return; } // 上面這一小段,以文本的方式,打開并讀寫文件。 ? ? QDomDocument doc; //xml,rtf 等多種文本格式生成所需要的類。 doc.setContent(&file); ? QDomElement root = doc.documentElement(); QDomElement body; if (!root.isNull()) { body = root.firstChildElement(QLatin1String("body")); //如果用戶保存的文件里面有內容, 查找是否是rtf格式的相關元素。 } if (body.isNull()) { root = doc.createElement(QLatin1String("html")); QDomElement head = doc.createElement(QLatin1String("head")); QDomElement meta = doc.createElement(QLatin1String("meta")); meta.setAttribute(QLatin1String("http-equiv"), QLatin1String("Content-Type")); meta.setAttribute(QLatin1String("content"), QLatin1String("text/html; charset=utf-8")); //rtf格式就是一個html格式文件, 一樣的結構, 需要文件頭, body內容。 head.appendChild(meta); root.appendChild(head); body = doc.createElement(QLatin1String("body")); root.appendChild(body); doc.appendChild(root); } // 上面這一小塊主要寫文件的文件頭部內容。 ? if (body.isNull()) return; ? QDomElement subtitleParagraph = doc.createElement(QLatin1String("p")); QDomText subtitleText = doc.createTextNode(tr("Subtitle:")+QLatin1String("this is a test")); // 上面這一小塊, 通過一個普通的p標簽,來實現body 的一部份。 ? subtitleParagraph.appendChild(subtitleText); ? QDomElement tempLabel = doc.createElement(QLatin1String("span")); tempLabel.setAttribute(QLatin1String("style"), QLatin1String("color:red")); QDomText tempNode = doc.createTextNode(QLatin1String(" this is red")); tempLabel.appendChild(tempNode); subtitleParagraph.appendChild(tempLabel); // 上面這一段內容,則是在p標簽后, 加入一個有文本樣式的字<span>標簽 , 把里面的字符串內容變為紅色。 ? body.appendChild(subtitleParagraph); ? QTextStream out(&file); doc.save(out, 4); file.close(); } 4.運行效果: a.用普通寫字板, 記事本打開效果為: ? 用wps 或word打開的效果為:總結
以上是生活随笔為你收集整理的QT 编写Rtf(富文本格式) 文件实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高通9x07平台linux,【产品】基于
- 下一篇: c语言 转换字母 顺序结构,C语言程序设