Important: Please read the Qt Code of Conduct -

Saving and loading rtf documents

  • I know this has been asked a few times because I have been trawling through the posts for a couple of days looking for an answer.

    I would like to write an app that loads and saves rtf documents.

    The reason to use rtf is that most word processors new and old can load this format.

    I wish to write book writing software.
    The software I used to use is Liquid Story binder and I wish to write something like that, but improve it.

    However it would be handy if I could save and load in rtf, and maybe other formats later.

    I know that QT cannot do this, so is there some kind of special libary I can get that will let me achieve this.

    I know that I can't be the only one that wants to do this, and the only post I have found on here on the subject is from 2012.

    Saving to html is not an option because people who are used to Liquid Story binder expect it to save in rtf, and I want it to have the same functionality.

  • Lifetime Qt Champion


    The big question is what verion/spec format of RTF do you mean?
    If the good old MS one, then
    could maybe be interesting.

    The RTF (old) ones are not very complex
    But would take long time to write your own since you need to support all constructs.

    Also since you are planning a full editor app, i would suggest to look at existing ones
    or maybe from Libra office.

  • The librtf library looks interesting, and should be able to make rtfs.
    What I don't know is how I would take the rtf and display i t in a QTextview, or how to save a QTextview to the rtf.
    It looks like a standard C++ library, but I am not sure how I would use that with the QTextview.

    You see people who write books like to use software for this kind of thing, but they have to know if they software fails for some reason, or they decide to change their operating system that they can get access to their work without the software if you know what I mean.

    I like QT and I have only made 2 applications with it yet, but this seems a little bit difficult heh heh.
    I wonder if anyone else on this board has done this before and maybe has some sample code I could look at?

  • Lifetime Qt Champion

    That why ask i ask what "RTF" u are talking about as RTF just means rich text.
    Microsoft have a really know RTF format but Qt uses HTML for rich text

    So its hard to use MS RTF directly as you would need to convert back and forth between
    .rtf and HTML. And its not super easy. Many details.

    I could not find anything that DRAWS an ms.RTF

    • You see people who write books like to use software for this kind of thing, but they have to know if they software fails for some reason, or they decide to change their operating system that they can get access to their work without the software if you know what I mean.

    Yes, i worked for a publishing house for 10 years. Its critical to use a common format.

    If you plan on making such super book editor then maybe look at open source editors to get an
    idea. Its huge work.

  • Yes I meant the rtf file format and not the Rich Text Format that QT uses.
    I think that was the MS rtf. I know it can be opened in MS office and open office in linux, so I thought it would be the best format to use.

    I spent a few years using Liquid Story Binder, but the software is getting old now, but a lot of people still like it. There is also Scrivener which is ok, but not quite as good as LSB, so I thought I would try and build something in-between the two.

    So before I start this task, I just needed to know if it is possible to export rtf format documents, and later to be able to export other doc formats. MS RTF would be the main one though.

    I have been programming in many languages over the years, so I know I can make the app ok, but the problem is that I don't know much about document formats.

    I knew this part would be the difficult part of designing it, and I was hoping there was a pre-built library for saving and loading doc formats into Qtextview.

    I would be happy right now just to get ms rtf to save and load.

    I think Qtextview can save in html, and pdf at the moment, but what other formats can it save as standard?

  • Lifetime Qt Champion

    as far as i know only save it html. ( and print to pdf)
    Maybe you cheat a bit. there should be rtf to html converts etc that might help.

    But yes the format will be a huge work. And then all the other features.

  • I have to say I like QT, but I am a little disappointed that there isn't an easier way of doing this.
    I didn't want to put a lot of work into this until I knew I could do the hard part which is the saving and the loading.

    I think my only option in this regard is to learn how rtf files are made, and then take the html from a QTextview and work out how to change it to rtf encoding.
    Probably will be a nightmare.

    Now I am not sure what to do, because it isn't really solved. Do I leave this as unsolved, or is there a setting to say it can't be solved heh heh.

  • Lifetime Qt Champion

    Well the MS RTF format is a old school now in the internet age
    so i guess thats why html was chosen for the universal format.

    However, many tools have been developed over the time and you can most likely get most
    as reusable code
    But you also need the other way

    So yes, using ms.RTF is not super easy as it not really used in Qt.

    You can just leave it unsolved for some days.

    There might be others with inputs or even code.

    You could also dig up source code for kword
    it handled rtf as far as remember.
    And its Qt.

  • @mrjj
    I have just come across this page

    ...and if you scroll down and look at individual formats it says it can do Microsoft's "RTF" format
    Although if you click on it there is no information.

    So you could think from looking at this page that QT can do ms rtf, or is the page wrong?

  • Lifetime Qt Champion

    Well, i think it was just listed by accident
    I have not seen Qt been able to read rtf.

    When i first started with with Qt i though the rich text would be ms.rtf so i tried to stuff a file
    into textedit and in that regard learned that "RTF" dont have to mean ms. rtf.

    I checked with ancient Qt3 and its still html so it dont look like it was ever a format that Qt could load.

    But I could be wrong :)

  • Well the good news is that the file loader will load an rtf eg

    void MainWindow::on_pushButton_clicked()
        QString file = QFileDialog::getOpenFileName(this,"Open a file");
            QFile sFile(file);
            if( | QFile::Text)){
                QTextStream in(&sFile);
                QString text = in.readAll();
            // ui->textEdit->setHtml(text);

    Obviously it loads all the rtf tags straight in. So if I was going to write a rtf decoder I would have to take each tag and convert it into the html equivalent, which I know won't be easy, but I have the whole file in the variable text.

    I went into ms word and saved one line as an rtf, and for some reason when you look through the tags in the rtf document it seems to be setting the font about 20 times LOL

    Wouldn't it be cool if I could just do


    Can you add your own commands to QT?

    Anyway in theory you could find out the rtf command for bold, then read it and then set it bold using html before sending it to the Qtextedit window, but this could be rather difficult heh heh.

    So I am going to do a lot more research. If I find an answer I will post it here. Would be cool if I could mark this as solved LOL

  • Lifetime Qt Champion

    @Asimov said in Saving and loading rtf documents:


    The function will accept any text. including rtf as it just sees it as text.

    • Can you add your own commands to QT?
      Yes. ofc.
      OFten you just subclass textEdit and add it.

    You can get much of the code for convert from here

  • @mrjj
    I looked at rtf2html, but there was a hell of a lot of h file and cpp files and it was hard for me to decypher what was needed and what wasn't.

    I have found a handy little aricle for on code project


    This guy has broken down simply some of the commands needed to make an rtf. Seems he has only scraped the surface, but he has the main tags, eg bold, underline and stuff.

    I have got to thinking that I don't need to know all the rtf commands there are. I just need to know the ones I am going to use to generate my rtf.

    For instance I need to know how to make text bold,italic, new paragraph and the like and then I can generate an rtf document that word can read.

    The only thing I have to work out now is how to parse the html commands so that I can turn them into the equivalent rtf tags.

    I am thinking of some kind of array to hold all the html commands, and then some how when I get an html tag for bold swap it for the rtf tag for bold.

    This could be difficult as I haven't done much string parsing in c++, but I have done this kind of thing in php.

    PS. Probably not going to be as simple as I thought as the QT html markup is overkill
    For instance I put one text line in and the html output was this

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "">
    <html><head><meta name="qrichtext" content="1" /><style type="text/css">
    p, li { white-space: pre-wrap; }
    </style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">kjkjhkjhkhkhk</p></body></html>

    That is what I call overkill html markup. If I was putting this in an actual webpage I would just do <p>fsdfsfsdf</p> so I am not sure why there is all this margin stuff. If you want a margin of zero that is usually the default.

  • Lifetime Qt Champion

    How much of RTF you need to support ?
    Table also ?
    Images ?
    What about style sheets/styles

    if you look in
    It has many features and i thinks its very important to set the goal of how much of it you want to support before
    starting to think about coding it.

    • but there was a hell of a lot of h file and cpp files and it was hard for me to decypher what was needed and what wasn't.

    Yes a rtf parser is not trivial. All the rtf_xx files are needed. You could also just use it as is.
    use QProcess to call it do behind the scene conversion.

  • At first I only want a few functions like bold,italic,underline. Later I may want to add images as well, but I would be happy with just text at first, after all it is writing software.

    I have made simple rtfs just with coding as a test, but the problem isn't that. It is parsing the QT Html, as it seems to add a lot of markup which isn't needed, and it also has markup which doesn't seem to exist in normal html like


    So even if I got a c++ libary to work it would never have that command in there, as it isn't standard html.

    I am slowly thinking maybe I should start writing the software with using the html save at first, and then working out if I can add rtf later. I was going to read in rtf and save out rtf, but perhaps I can limit the rtf to an export button or something.

    Reading in an rtf I created wouldn't be so bad. I can read the code for bold in rtf and turn it into <b> for Html, but to be honest I only wanted to add rtf support so that the files are compatible with ms word, or any other word processor.

    Stupid thing is that I found out that word can actually load Html files (something I didn't know before.)
    But people who use this kind of software have no idea that you can load an html into word, and they would probably cry that there is no rtf export LOL.

  • Lifetime Qt Champion

    AS far as i Know its 100% valid HTML
    But i never test it though :)

    I agree since it will be html internally, then you might as well wait with the RTF stuff as its likely export function.
    Also u can just cheat and call a external commandline app. plenty exists.

    Btw you could also generate MS xml word files. but then again, u if you need to import it again , that will be complicated also.
    Yes word have loaded html since 2003 :) (also before but that was awful)

Log in to reply