PDA

View Full Version : Printing some data



cyberboy
9th April 2008, 16:43
Hi guys,

I'm currently busy with a printing function for my program. But I can't get some scales right.

The problem is that the text is parsed on one line and when the paper ends it doesn't start at a new line?

This is my current code


#include "print.h"
#include <QPrinter>
#include <QPrintDialog>
#include <QPainter>

void print::printOrder(const QStringList client, const QStringList data){
//generate printing instance
QPrinter printer;
printer.setPageSize(printer.A4);

//create print dialog, pass printer instance
QPrintDialog *pDlg = new QPrintDialog(&printer, this);
pDlg->setWindowTitle(tr("Bestelling printen"));

//debugging
printer.setOutputFileName("rtest.pdf");


if(pDlg->exec())
return;
qDebug("printen");


//generate QPainter instance
QPainter painter;

//pass printer reference to the painter.begin() function
painter.begin(&printer);

painter.setPen(Qt::black);
painter.setFont(QFont("Arial", 14));

painter.drawText(rect(), Qt::AlignLeft, "Bestelling overzicht");
painter.translate(0, 20);

int count = client.size();

for(int i = 1; i < count; i++){
painter.drawText(rect(), Qt::AlignLeft, client.at(i).toLocal8Bit().constData());
painter.translate(0, 20);
}

painter.translate(0, 40);
painter.drawText(rect(), Qt::AlignLeft, "Bestelling");
painter.translate(0, 20);

int countData = data.size();

for(int i = 1; i < countData; i++){
painter.drawText(0, 0, 100, 100, Qt::AlignLeft, data.at(i).toLocal8Bit().constData());
painter.translate(0, 20);
}

painter.save();


}

void print::printWorkingOrder(){

}


The text is parsed in the multiple lines except for the text in the QStringList that is to long to parse on the page and some how doesn't want to be parsed on multiple lines?

Does somebody know how to parse long lines of text within the boundaries of the paper on multiple lines?

Greets,

Cyberboy

cyberboy
28th April 2008, 14:08
After a little break and a clean mind I went further with the printing code.

This is what i've got until now :


void print::printOrder(const QStringList client, const QStringList data){
//generate printing instance
QPrinter printer;
printer.setPageSize(printer.A4);

//create print dialog, pass printer instance
QPrintDialog *pDlg = new QPrintDialog(&printer, this);
pDlg->setWindowTitle(tr("Bestelling printen"));

//debugging
printer.setOutputFileName("rtest.pdf");

//get page scales
const double xScale = printer.pageRect().width();
const double yScale = printer.pageRect().height();

double yPos = 0;
double xPos = 0;

qDebug("xScale:%f",xScale);
qDebug("yScale:%f", yScale);

if(pDlg->exec())
return;



//generate QPainter instance
QPainter painter;

//pass printer reference to the painter.begin() function
painter.begin(&printer);

//set scale

painter.setPen(Qt::black);
painter.setFont(QFont("Arial", 14));

painter.drawText(rect(), Qt::AlignLeft, "Bestelling overzicht");
painter.translate(0, 20);
yPos += 20;

int count = client.size();

for(int i = 1; i < count; i++){
QRectF rectt = painter.boundingRect(QRect(), Qt::AlignLeft, client.at(i).toLocal8Bit().constData());

qDebug("height:%f", rectt.height());
qDebug("width:%f", rectt.width());


yPos += rectt.height();

if( yPos < yScale ){

painter.drawText(rect(), Qt::AlignLeft, client.at(i).toLocal8Bit().constData());
painter.translate(0, rectt.height());

}else{
//new print page
printer.newPage();
painter.resetTransform();
yPos = 0;

painter.drawText(rect(), Qt::AlignLeft, client.at(i).toLocal8Bit().constData());
painter.translate(0, rectt.height());

}
}

painter.drawText(rect(), Qt::AlignLeft, "Bestelling");
painter.translate(0, 20);
yPos += 20;

int countData = data.size();

for(int i = 1; i < countData; i++){
QRectF rectt = painter.boundingRect(QRect(), Qt::AlignLeft, data.at(i).toLocal8Bit().constData());

qDebug("height:%f", rectt.height());
qDebug("width:%f", rectt.width());

yPos += rectt.height();

if( yPos < yScale){

painter.drawText(rect(), Qt::AlignLeft, data.at(i).toLocal8Bit().constData());
painter.translate(0, rectt.height());

}else{

printer.newPage();
painter.resetTransform();
yPos = 0;

painter.drawText(rect(), Qt::AlignLeft, data.at(i).toLocal8Bit().constData());
painter.translate(0, rectt.height());


}

}

painter.save();
}


And it works, the y coordinates and the translations works fine.
I can check if the text length is to big but what then, I don't know how to chop the text in half and start on a newline?

Can somebody help me with that?

Greetz,

Cyberboy