Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Saving and loading rtf documents
QtWS25 Last Chance

Saving and loading rtf documents

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 2 Posters 8.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asimov
    wrote on 30 Apr 2017, 17:39 last edited by
    #7

    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.

    M 1 Reply Last reply 30 Apr 2017, 18:23
    0
    • A Asimov
      30 Apr 2017, 17:39

      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.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 30 Apr 2017, 18:23 last edited by mrjj
      #8

      @Asimov
      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
      Like
      https://github.com/lvu/rtf2html
      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
      https://en.wikipedia.org/wiki/KWord
      it handled rtf as far as remember.
      And its Qt.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Asimov
        wrote on 1 May 2017, 01:41 last edited by
        #9

        @mrjj
        I have just come across this page
        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?

        M 1 Reply Last reply 1 May 2017, 06:07
        0
        • A Asimov
          1 May 2017, 01:41

          @mrjj
          I have just come across this page
          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?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 1 May 2017, 06:07 last edited by
          #10

          @Asimov
          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 :)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asimov
            wrote on 1 May 2017, 14:10 last edited by Asimov 5 Jan 2017, 14:11
            #11

            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");
                if(!file.isEmpty()){
                    QFile sFile(file);
                    if(sFile.open(QFile::ReadOnly | QFile::Text)){
                        QTextStream in(&sFile);
                        QString text = in.readAll();
                        sFile.close();
            
                        ui->textEdit->setAcceptRichText(true);
                     ui->textEdit->setPlainText(text);
                    // 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

            ui->textEdit->setRTF(text);
            

            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

            M 1 Reply Last reply 1 May 2017, 14:16
            0
            • A Asimov
              1 May 2017, 14:10

              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");
                  if(!file.isEmpty()){
                      QFile sFile(file);
                      if(sFile.open(QFile::ReadOnly | QFile::Text)){
                          QTextStream in(&sFile);
                          QString text = in.readAll();
                          sFile.close();
              
                          ui->textEdit->setAcceptRichText(true);
                       ui->textEdit->setPlainText(text);
                      // 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

              ui->textEdit->setRTF(text);
              

              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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 1 May 2017, 14:16 last edited by
              #12

              @Asimov said in Saving and loading rtf documents:

              setPlainText

              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
              https://github.com/lvu/rtf2html

              1 Reply Last reply
              2
              • A Offline
                A Offline
                Asimov
                wrote on 1 May 2017, 23:11 last edited by Asimov 5 Feb 2017, 00:01
                #13

                @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 vb.net on code project

                Article

                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" "http://www.w3.org/TR/REC-html40/strict.dtd">
                <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.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 2 May 2017, 07:07 last edited by
                  #14

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

                  if you look in
                  https://github.com/lvu/rtf2html/blob/master/rtf_keyword.cpp
                  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.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Asimov
                    wrote on 2 May 2017, 18:30 last edited by
                    #15

                    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

                    qt-block-indent:0;
                    

                    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.

                    M 1 Reply Last reply 2 May 2017, 18:39
                    0
                    • A Asimov
                      2 May 2017, 18:30

                      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

                      qt-block-indent:0;
                      

                      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.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 2 May 2017, 18:39 last edited by
                      #16

                      @Asimov
                      AS far as i Know its 100% valid HTML
                      http://doc.qt.io/qt-5/richtext-html-subset.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)

                      1 Reply Last reply
                      0

                      16/16

                      2 May 2017, 18:39

                      • Login

                      • Login or register to search.
                      16 out of 16
                      • First post
                        16/16
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved