PDA

View Full Version : QPrinter problems



Teuniz
30th October 2007, 14:52
Hello,

It seems that it is almost impossible to get consistant printing results with
QPrinter class on Linux and Windows and with different kinds of printers.

Is QPrinter just not ready for professional use or am I doing something wrong?

I created a QPainter widget and draw some curves on the screen. Then I want
to print this to a printer, a postscriptfile and a pdf-file.

Here's a part of my code:

void ViewCurve::print_to_printer()
{
QPrinter printer(QPrinter::ScreenResolution);

printer.setOutputFormat(QPrinter::NativeFormat);
printer.setOrientation(QPrinter::Landscape);
printer.setFullPage(FALSE);
printer.setPageSize(QPrinter::A4);
printer.setCreator("Printertest");

// printf("resolution is %i\n", printer.resolution());
// printf("width is %i\n", printer.pageRect().width());
// printf("height is %i\n", printer.pageRect().height());

// printer.setResolution((int)((double)width() / 10.6));

#ifndef WIN32
// printer.setPrintProgram("lpr");
#endif

QPrintDialog printerdialog(&printer, this);
printerdialog.setWindowTitle("Print");

if(printerdialog.exec()==QDialog::Accepted)
{
QPainter paint(&printer);

drawCurve(&paint);
}
}


void ViewCurve::print_to_postscript()
{
// double width_factor;

QFileDialog fchooser;

QStringList fileNames;

fchooser.setViewMode(QFileDialog::Detail);
fchooser.setConfirmOverwrite(1);
fchooser.setFileMode(QFileDialog::AnyFile);
fchooser.setAcceptMode(QFileDialog::AcceptSave);
fchooser.setWindowTitle("Print to PostScript");
fchooser.setLabelText(QFileDialog::FileName, "File");
fchooser.setDefaultSuffix("ps");
#ifndef WIN32
fchooser.setDirectory(getenv("HOME"));
#endif
fchooser.setFilter("PostScript files (*.ps *.PS)");
fchooser.selectFile("printertest.ps");

if(!(fchooser.exec() == QDialog::Accepted))
{
return;
}

fileNames = fchooser.selectedFiles();

QPrinter printer(QPrinter::ScreenResolution);

printer.setOutputFormat(QPrinter::PostScriptFormat );
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFileName(fileNames.at(0));

// printer.setResolution((int)((double)width() / 10.6));


// printf("resolution is %i\n", printer.resolution());
// printf("width is %i\n", printer.pageRect().width());
// printf("height is %i\n", printer.pageRect().height());

// width_factor = ((double)printer.pageRect().width()) / ((double)width());

QPainter paint(&printer);

// drawCurve(&paint, printer.pageRect().width(), printer.pageRect().height(), width_factor);
drawCurve(&paint);
}


void ViewCurve::print_to_pdf()
{
QFileDialog fchooser;

QStringList fileNames;

fchooser.setViewMode(QFileDialog::Detail);
fchooser.setConfirmOverwrite(1);
fchooser.setFileMode(QFileDialog::AnyFile);
fchooser.setAcceptMode(QFileDialog::AcceptSave);
fchooser.setWindowTitle("Print to PDF");
fchooser.setLabelText(QFileDialog::FileName, "File");
fchooser.setDefaultSuffix("pdf");
#ifndef WIN32
fchooser.setDirectory(getenv("HOME"));
#endif
fchooser.setFilter("PDF files (*.pdf *.PDF)");
fchooser.selectFile("printertest.pdf");

if(!(fchooser.exec() == QDialog::Accepted))
{
return;
}

fileNames = fchooser.selectedFiles();

QPrinter printer(QPrinter::ScreenResolution);

printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileNames.at(0));
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);
// printer.setResolution((int)((double)width() / 10.6));

QPainter paint(&printer);

drawCurve(&paint);
}
If I choose QPrinter::HighResolution instead of QPrinter::ScreenResolution,
I need to set the resolution manually because the size of the widget is
dependant of the screen, for example 1280 x 1024 or 1024 x 768. This
depends on the user ofcourse. Even then, I get different sizes on different
printers. One printer prints OK, on another one there is a part missing.

For reasons, I can not repaint the widget on a (bigger) fixed scale.

According to the documentation of Qt 4.3.2, using QPrinter::ScreenResolution
should do the job but in reality, it prints wrong sizes.

I attached a complete Qt testproject including the .pro file.

Are there any people with the same experience. Is there any solution?

I'm using OpenSUSE 10.2 and Windows XP.

Thanks in advance,

Teuniz

Teuniz
3rd November 2007, 08:51
I found the solution. I choose HighResolution for the printer. Then I ask for the printable area and then I scale the widget on which I will paint so that it fits
into the printable area. Like this:


QPrinter printer(QPrinter::HighResolution);

printer.setOutputFormat(QPrinter::NativeFormat);
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);

#ifdef Q_WS_X11
printer.setPrintProgram("lpr");
#endif

width_factor = ((double)printer.pageRect().width()) / ((double)width());
height_factor = ((double)printer.pageRect().height()) / ((double)height());

QPainter paint(&printer);

paint.scale(width_factor, height_factor);

drawCurve(&paint);

Strange that nobody here could point me to this...

Regards,

Teuniz