PDA

View Full Version : Creating documents from Qt



GrahamLabdon
23rd April 2010, 08:28
Hi all
I am working on a project where we need to produce documents (in a platform independent way) from data held withing the application.
Does anyone have any recomendations on how this can be achieved?

TIA

Graham

Lykurg
23rd April 2010, 08:43
What kind of documents? TXT files are platform independent;) No seriously, why not using pdf for that?

EDIT: For pdf use a QPainter on a QPrinter and save that as pdf.

GrahamLabdon
23rd April 2010, 08:49
Hi - thanks for replying
Forgive my ignorance, I am completely new to QT.
I am currently redesigning/rewriting an exiting MS application to use QT so we can take advantage of platform and language independence that QT offers.
Currently the application produces a simplistic plain text document, but our customers want a professional looking document- currently they use our data to produce a MS Word document.

How easy is it to produce a PDF document? (how does one do it)?
Is there an advantage to using something like ODF? (would our customers be able to read the document)

TIA
Graham

Lykurg
23rd April 2010, 08:57
Well it depends what your customer want to do with the document. If don't want to change it, I would go for pdf, since then it is guaranteed, that it look everywhere the same and you can store the document a long time. If you want to change values then of course go for odf. But it sounds like your costumer only wants something to print out.

For pdf there are some examples, but effectively it is nothing else than painting (using QPainter) on a defined piece of paper where you have to positioning the elements. One nice thing is that you can also use a QTextDocument for more advanced text documents and print it directly using QTextDocument::print().

Lykurg
23rd April 2010, 09:02
I don't know if you have found that: http://doc.trolltech.com/qq/qq27-odfwriter.html. It's an good article for starting. Even if it is for odt.

wysota
23rd April 2010, 09:38
A good idea is to render the document into html and then print it to pdf from WebKit using means already mentioned by Lykurg.

GrahamLabdon
23rd April 2010, 09:39
Thanks everyone

GrahamLabdon
23rd April 2010, 11:10
Hi everyone
I used the code from the QT Quarterly suggested by Lykurg to produce a odt document.
However when I try to open with MS Word it refuses as it says that the file couild not be opened - 'the open office XML .. file could not be opened' the details tab gives 'the file is corrupt and could not be opened'

Any ideas?

Graham

wysota
23rd April 2010, 11:19
MS Word can't open ODT files without a dedicated filter.

GrahamLabdon
23rd April 2010, 11:25
Hi wysota

What filter do I need?

Lykurg
23rd April 2010, 12:31
Ask google for "word odf"... E.g. http://sourceforge.net/projects/odf-converter/

GrahamLabdon
23rd April 2010, 14:08
Thanks Lykurg
I have downloaded the odf-converter and can now open the generated odt file, however, the formating is all lost and does not look like the example given
any more ideas?
Graham

wysota
23rd April 2010, 16:29
Do you require the document to be editable in Word or is it just something you would like to have?

GrahamLabdon
26th April 2010, 08:09
The document should be editable in word and preferably on MAC and *nix

wysota
26th April 2010, 09:31
*should* or *has to*? If it has to be editable on all of those platforms then your only choice is RTF (http://en.wikipedia.org/wiki/Rich_Text_Format). Just know there is no direct support for it in Qt.

GrahamLabdon
26th April 2010, 09:39
Hi
I am still playing with odt documents.
I have created a document within a QT application and am using OpenXML/ODF Translator Add-in for Office to convert it.
The document contains a table and an image.
When I open the document I see the plain text and the table, but the format I applied to the table is not visible (solid borders) and I see no image. Do you have any ide why this is?

Code for table


m_cursor.insertText(QObject::tr(
"Phone bill for %1\n").arg(client));

QTextTableFormat tableFormat;
tableFormat.setBorderStyle(
QTextFrameFormat::BorderStyle_Double);
m_cursor.insertTable(1, 3,tableFormat);
m_cursor.insertText(QObject::tr("Date"));
m_cursor.movePosition(QTextCursor::NextCell);
m_cursor.insertText(QObject::tr("Duration (sec)"));
m_cursor.movePosition(QTextCursor::NextCell);
m_cursor.insertText(QObject::tr("Cost"));


Code for image


const int columnSize = 10;
int width = values.count() * columnSize;
int max = 0;
foreach (int x, values)
max = qMax(max, x);
QImage image(width, 100, QImage::Format_Mono);
QPainter painter(&image);
painter.fillRect(0, 0, image.width(), image.height(),
Qt::red); // background
for (int index = 0; index < values.count(); ++index) {
// Adjust scale to our 100 pixel tall image:
int height = values[index] * 100 / max;
painter.fillRect(index * columnSize,
image.height() - height, columnSize, height,
Qt::black);
}
painter.end();

QTextCursor cursor(m_document);
cursor.movePosition(QTextCursor::End);
cursor.insertText(subtext);
cursor.insertBlock();


cursor.insertImage(image);


TIA

Graham

wysota
26th April 2010, 10:00
In my opinion you are just wasting your time. You will never be able to preserve complete formatting this way. You will lose some first when you export the document from Qt (as the odt writer is not 100% compliant with OpenOffice's odf reader) and then you'll lose some again trying to convert to Word. If you are after displaying the document then use PDF. If you are after editing it, use RTF. If you are after editing it and don't mind buying additional software, use PDF and use Acrobat (or equivalent) to edit the document.