PDA

View Full Version : Printing In QT



Splatify
7th March 2011, 19:25
Hi all I have multiple elements I wish to print.

I have a label, a textedit and I have a tableview full of data which I wish to print. I assume that I need to add all of these into a single document and then print the document. I was wondering how to add all of these items to a single document and then print it?

So far the code I have is:


//Select a printer
QPrinter printer;

QPrintDialog *dialog = new QPrintDialog(&printer, this);
dialog->setWindowTitle("Print Document");
dialog->exec();

//Create a text document:
QTextDocument doc;
// I think I need some code here which adds all the elements.
//This is the part I'm getting confused with.

//Print the document
doc.print(&printer);

Could someone please give me some guidance, perhaps some example code?


Thanks for your time and trouble

waynew
8th March 2011, 00:09
Splatify - I don't know how other folks print, but I am printing labels like this:


QFont printFont("courier", 10);
printFont.setFixedPitch(TRUE);

QPainter painter(&printer);
painter.setPen(Qt::black);
painter.setFont(printFont);

int x = 1;
int y = 20;
for (int i = 0; i < list.size(); i++)
{
painter.drawText(x, y, list[i]);
y = y + 15;
}

The list is built from a database query by appending the query values to a QStingList. It works fine for me. You should be able to use the same type of code by just appending whatever you need to your list.

This code comes after my call to the QPrintDialog.