Results 1 to 2 of 2

Thread: Display the rtf text in QTextEdit

  1. #1
    Join Date
    Sep 2017
    Qt products
    MacOS X Unix/X11

    Default Display the rtf text in QTextEdit

    {\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

    \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.

    Qt Code:
    1. QString filename="/home/clement/test.rtf";
    3. QFile file(filename);
    4. if ( | QIODevice::Text)) {
    5. ui->textEdit->setPlainText(file.readAll());
    6. }
    7. file.close();
    9. QTextDocument *document = ui->textEdit->document();
    11. QClipboard *board = QApplication::clipboard();
    12. QMimeData *mimeData = new QMimeData();
    13. mimeData->setText(document->toPlainText());
    14. board->setMimeData(mimeData, QClipboard::Clipboard);
    16. QString str = board->text();
    17. QMessageBox::information(NULL, tr("剪貼簿內容"), str);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 844 Times in 831 Posts
    Qt products

    Default Re: Display the rtf text in QTextEdit

    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.

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 31st October 2014, 07:32
  2. Replies: 2
    Last Post: 22nd September 2012, 01:18
  3. How to display text from a file to a text browser
    By ironmantis7x in forum Newbie
    Replies: 11
    Last Post: 14th June 2012, 15:23
  4. Replies: 9
    Last Post: 28th March 2011, 22:51
  5. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.