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. Simply creating a document in colour
QtWS25 Last Chance

Simply creating a document in colour

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 5.2k 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.
  • C Offline
    C Offline
    crispylx
    wrote on last edited by
    #1

    Hi all, I’m afraid I’m still quite new to Qt and C++ and would like some advice. I have my program that uses fstream to create a text file. It all works great. Now I want to create the same document but the text needs to be in colour. I’ve read up about QTextDocument but I’m hoping that someone can point me in the direction of a simple example to help me understand it better.
    Many Thanks
    Chris

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vcsala
      wrote on last edited by
      #2

      What do you mean that "text needs to be in colour"? Do you want to print it or diplay it in a widget?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        QTextDocument can contain rich text, HTML or plain text. The first two formats may contain color and format definitions. But the you also have to store it in that format (and never as toPlainText regarding QTextEdit).

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • C Offline
          C Offline
          crispylx
          wrote on last edited by
          #4

          VCsala - I’m looking to print it, I dont need to view it - actually I’m looking to save it - pdf, rtf would be fine. When the document prints it needs to print in 4 colours.

          Gerolf - Fantastic, I think I need to be working along the QTextDocument lines. Rich Text would work and would be able to Print out and save with coloured text (happy to save in rich text format - that would be ideal).

          Unfortunately, I’m really stupid and I cant work out the code to save a simple rtf file. I have all the data in memory, it is just a case of knowing how to save it. If anyone can point me in the direction of some example code, I would be very grateful.

          Thanks for your speedy replies. You’re very kind.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            You can use QTextDocumentWriter to output a QTextDocument to a supported format.

            Unfortunately, QTextDocument does not support RTF as an output format. Only plain text, HTML and ODF are supported. I did not find an easy way to add support for a format either, though I might have just overlooked this in the documentation. The API design seems to have been done in a way that this should be possible though.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              crispylx
              wrote on last edited by
              #6

              Andre - Just to confirm, I can have coloured text in an ODF text document?

              I’ll try at that, any examples would be a fantastic help

              Thanks

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Yes, ODF supports colored text. I am, however, not 100% positive that QTextDocumentWriter supports that feature of ODF, but that would be a simple test to make yourself, would it not?

                The documentation of QTextDocumentWriter is quite clear, and the interface quite simple, I think. Is there anything in there you don't understand specifically?

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  crispylx
                  wrote on last edited by
                  #8

                  Unfortunately, I’m at the really basic level. C++ I’m ok with but I’m not experienced with Qt. You need to treat me like an idiot I’m afraid. I’ve found the example from Nokia - creating a phone bill - which I’m working through.

                  Thanks for your replies

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Don't hesitate to ask specific questions if you stumble on problems working through the example.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #10

                      I just had a look at the code of QTextEdit which (per documentation) also accepts RichText format, built only for user input (drag and drop, copy/paste) :-(

                      SO I'm afraid, only plaintext, odf or html is possible (without extra coding)

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        vcsala
                        wrote on last edited by
                        #11

                        As the QTextDocument support HTML I am just wondering why do not you use it as your output format? As I understand your request most probably it can fit to your requirements.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          [quote author="Gerolf" date="1301914256"]I just had a look at the code of QTextEdit which (per documentation) also accepts RichText format, built only for user input (drag and drop, copy/paste) :-(

                          SO I'm afraid, only plaintext, odf or html is possible (without extra coding)[/quote]
                          That might actually warrant a feature request: add RTF to the list of supported formats for QTextDocumentWriter, and make it possible to add custom formats for that.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #13

                            Feel free to create a suggestion to Jira or make a code submission :-)
                            It would be a nice feature to have QTextDocumentWriter (or QTextDocument) plug-ins to be extendable here.

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #14

                              OK, fair enough:

                              • "Make list of supported QTextDocumentWriter formats extendible":http://bugreports.qt.nokia.com/browse/QTBUG-18570
                              • "Add support for RTF to QTextDocumentWriter":http://bugreports.qt.nokia.com/browse/QTBUG-18571
                              1 Reply Last reply
                              0

                              • Login

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