PDA

View Full Version : QPrinter output in portrait instead landscape in windows



jiveaxe
18th January 2008, 17:03
Hi,
I have the following code for printing to pdf a widget:


void MainWindow::magToPdf()
{
if(magazineViewer) {
QPrinter printer;
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFileName(magazineViewer->windowTitle() + ".pdf");
printer.setOutputFormat(QPrinter::PdfFormat);
QPixmap qpm = QPixmap::grabWidget(magazineViewer);
if(qpm.isNull()) {
QMessageBox::critical(0, "Error", "Can not capture the window", QMessageBox::Cancel);
return;
}
QPainter p;
p.begin(&printer);
p.drawPixmap(85, 32, qpm);
p.end();
}
}

In linux it works fine: the printed page is in landscape orientation but in windows acrobat shows it in portrait, cutting the right side of the captured widget.

Is it possible to resolve this behaviour?

Thanks

jpn
18th January 2008, 17:22
Could you attach a minimal compilable test application? As far as I remember, it worked just fine for me on Windows when I last happened to try.

jiveaxe
18th January 2008, 18:09
Here a small code with the same problem; press F8 to print.


#include <QApplication>
#include <QDialog>
#include <QtGui>

class Dialog: public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent=0);

private:
QLabel *label;
void keyPressEvent(QKeyEvent *event);
void magToPdf();

};

Dialog::Dialog(QWidget *parent)
:QDialog(parent)
{
label = new QLabel("QT Rules!");
label->setStyleSheet("font-size: 80px");

QHBoxLayout *horizontal = new QHBoxLayout;
horizontal->addStretch();
horizontal->addWidget(label);
horizontal->addStretch();

QVBoxLayout *vertical = new QVBoxLayout;
vertical->addStretch();
vertical->addLayout(horizontal);
vertical->addStretch();

setLayout(vertical);

setWindowTitle("Simple QPrint");
setFixedSize(900,667);
}

void Dialog::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_F8)
magToPdf();
}

void Dialog::magToPdf()
{
QPrinter printer;
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFileName(windowTitle() + ".pdf");
printer.setOutputFormat(QPrinter::PdfFormat);
QPixmap qpm = QPixmap::grabWidget(this);
if(qpm.isNull()) {
QMessageBox::critical(0, "Error", "Can not capture the window", QMessageBox::Cancel);
return;
}
QPainter p;
p.begin(&printer);
p.drawPixmap(85, 32, qpm);
p.end();
}


int main(int argc, char *argv[])
{
QApplication app( argc, argv );
Dialog dialog;
dialog.show();
return app.exec();
}

#include "main.moc"

jpn
18th January 2008, 18:36
Attached PDF was generated with Qt/Win 4.3.2.

jiveaxe
18th January 2008, 18:41
In windows I have QT 4.3.1 . Upgrading should solve the problem?

jpn
18th January 2008, 18:52
Sorry, I don't have Qt 4.3.1 installed anymore so I cannot test the difference myself. But as I said, it works for me with Qt 4.3.2. ;)