Simply creating a document in colour
-
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 -
What do you mean that "text needs to be in colour"? Do you want to print it or diplay it in a widget?
-
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).
-
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.
-
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.
-
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
-
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?
-
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
-
Don't hesitate to ask specific questions if you stumble on problems working through the example.
-
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)
-
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.
-
[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. -
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. -
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/14