PDA

View Full Version : Rotating Printing of QGraphicsScene



init2null
20th March 2008, 18:15
Hello all,

I'm working on a program that needs to print images of documents, and I'm stumped on how to rotate the contents to print sideways, so as to fill all available space. In my scene, I have an image (which defines the size of the scene) and various primitives. Centering and scaling the document in proportion on the page are my goals, but getting this to work would be a great start. The code I'm trying here only moves the image partway in from the left, cutting off part of it. Any ideas?



bool AreaWindow::showPrintDialog() {
QPrinter printer(QPrinter::HighResolution);
QPrintDialog printDialog(&printer, this);
printDialog.setMinMax(1, 1);
if (printDialog.exec() == QDialog::Accepted) {
QPainter painter(&printer);
if (image.width() > image.height()) {
painter.rotate(90);
painter.translate(0, -image.height());
}
scene->render(&painter);
return true;
}
return false;
}

wysota
20th March 2008, 19:50
QPainter::setWindow() and QPainter::setViewport() are your friends for the task. Or QPainter::scale() if you want to calculate the scale yourself.

init2null
21st March 2008, 20:27
Thanks for the suggestion, wysota, but I've worked on it for hours and haven't made any progress using them. For the present situation, I assumed that setWindow would work to bring the rotated image back into view by setting the window to (-image.width(), 0, image.width(), image.height()). I've tried this followed by rotating it 90 degrees (blank page) or rotating to 80 (a rotated piece of the image, with it cutting short in midair). I realize I need to set it properly to keep aspect, but I'm not worrying about that yet. I'd also really like to know why "painter.translate(0, -image.height());" isn't enough.

wysota
21st March 2008, 21:47
Could be that your printer has different horizontal and vertical resolution or your scene has different proportions than the paper.

init2null
22nd March 2008, 02:42
Well, I still don't have an answer as to why rotating doesn't work. I don't think the problem is resolution- or aspect-related, but I think I've found a way to circumvent it by just using QPrinter::setOrientation. It seems to do everything I need it to do, including scaling. I should've found it sooner, but I got stuck thinking along this path. Thanks for your help though!