PDA

View Full Version : Display the rtf text in QTextEdit



ng2b30
20th March 2018, 09:40
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf13 0
{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fswiss\fcharset0 ArialMT;\f2\fnil\fcharset0 LucidaGrande;
}
{\colortbl;\red255\green255\blue255;\red0\green0\b lue255;}
\paperw11900\paperh16840\vieww12000\viewh15840\vie wkind0
\deftab720
\pard\pardeftab720\partightenfactor0

\f0\b\fs28 \cf0 License Agreement
\f1 \'96 Term of Use
\b0\fs22 \
\

}

The above is the rtf text example.
How can i show rtf file in QTextEdit in qt?
Can I use QClipboard to convert the text to html and paste it in the QTextEdit like the following code? Thank you very much.


QString filename="/home/clement/test.rtf";

QFile file(filename);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
ui->textEdit->setPlainText(file.readAll());
}
file.close();

QTextDocument *document = ui->textEdit->document();

QClipboard *board = QApplication::clipboard();
QMimeData *mimeData = new QMimeData();
mimeData->setText(document->toPlainText());
board->setMimeData(mimeData, QClipboard::Clipboard);

QString str = board->text();
QMessageBox::information(NULL, tr("剪貼簿內容"), str);

d_stranz
20th March 2018, 17:26
The clipboard does not have any functions to convert formats. You will have to use an external library for conversion. If you Google for "C++ convert rtf html" you will find many links. Here is one of them. (http://www.ireksoftware.com/RTFtoHTML/)

Also understand that QTextEdit does not support all of HTML, so even if you can convert the RTF to HTML, it may not display correctly if there are unsupported HTML elements in it.