PDA

View Full Version : Printing a textdocument via QStringLists



gamenovice
8th May 2012, 03:27
Hello all,

I feel very confused about printing.

Everywhere I went, it was too confusing figuring out where to start when to print out a simple document.

I wanted to print a document like this:

I press the print button, it opens up a dialog checking pdf or not.

I have this QStringList of all Items sorted in my inventory


QStringList items:

each element looked like this: "title,type,author,datePublished"

what I wanted to do was print out the report to a pdf in a way that looked like this:


LIBRARY ITEMS : DATE

items[0]
items[1]
items[2]
items[3]
items[4]

etc..

But all of this about QPainter, QTextdocuments, QPrintPreviewDialog/Widget (list goes on and on).... not to mention every attempt i have done ends saying the Qpainter hasn't started(isnt that what the begin method is for!?)

I sound like an idiot, I know.

But if anyone could help with this, it would be a great help.

I could use some help in how to tell when to go to the next page... i could never understand that from the docs

all i need is a general guideline on how to implement this simple list via printing out qstringlists to what seems to be either the painter directly or document or.... honestly i dont know. I'm so confused.

Thanks for those who help,
gamenovice

**let me know if i was not clear

ChrisW67
8th May 2012, 07:08
How about you show us what you have that gives you there error you describe and then we can start to help.

Two obvious approaches:

Create a QTextDocument, using QTextCursor or by generating and importing HTML, and then use its print() function
Manually paginate your data and render it line by line to a QPainter attached to the QPrinter.

gamenovice
8th May 2012, 20:08
about that first option:

could I simply edit my text via the Textcursor, and when everything is done I can call the print function without anything like the paint event?

also, could you embed html tags into the insert text as well as the '\n' character sequence, or would that work?
(I know there is a sethtml function, but I'm curious if insertText can support on the fly html)

ChrisW67
8th May 2012, 23:32
could I simply edit my text via the Textcursor, and when everything is done I can call the print function without anything like the paint event?

Yes.

also, could you embed html tags into the insert text as well as the '\n' character sequence, or would that work?
(I know there is a sethtml function, but I'm curious if insertText can support on the fly html)
QTextCursor::insertHTML() looks promising.

If you haven't already, have a read of Rich Text Processing

If you can get hold of a copy of Advanced Qt Programming by Summerfield you will find a whole chapter that does just this sort of thing.

gamenovice
9th May 2012, 02:03
one more thing, is there anyway to get a QTextDocument to go to a printPreview and then via dialog, go on to actual printing?

Thank you for your help btw.

ChrisW67
9th May 2012, 03:13
Yes. The QPrintPreviewDialog::paintRequested() (QPrintPreviewWidget) signal passes a QPrinter* that renders to the preview. Connect that signal to a slot that calls QTextDocument::print() passing the QPrinter*.