PDA

View Full Version : QPrintPreviewDialog help



Yaoming
25th February 2014, 16:44
Hello there,
I know there are a lot of topics about QPrintPreviewDialog issues, I read a lot of them but it didn't help me so here I am. I need some explanations because I don't understand how it works.
I've got two QPushButtons that will call either the print preview or the print itself.
Here's my code so far:


//My printPreview slot
QPrinter printer(QPrinter::HighResolution);
QPrintPreviewDialog preview(&printer, this);
preview.setWindowFlags ( Qt::Window );
preview.setWindowTitle("Aperçu avant impression");
connect(&preview, SIGNAL(paintRequested(QPrinter *)), SLOT(Impression(QPrinter *))); //Impression is my print slot. When is paintRequested emitted exactly? I read the documentation but didn't understand....
preview.exec();


//My Print slot
QPainter painter;
painter.begin(p); //p is my QPrinter
double xscale = p->pageRect().width()/double(ui.Onglets->width());
double yscale = p->pageRect().height()/double(ui.Onglets->height());
double scale = qMin(xscale, yscale);
painter.translate(p->paperRect().x() + p->pageRect().width()/2, p->paperRect().y() + p->pageRect().height()/2); // to put my widget in the center of my page
painter.scale(scale, scale);
painter.translate(-width()/2, -height()/2);

ui.Onglets->render(&painter); //Onglets is a QTabWidget


So far only a part of my window shows up in my preview dialog. This problem comes from my print slot or the print preview one?
Could you please explain to me how it works exactly?
Thanks in advance for your answers.

ChrisW67
25th February 2014, 23:44
The problem is in your Impression(QPrinter *) slot. You determine the scale based on the width() and height() of ui.Onglets. When you translate to the centre, scale, and translate back using the different width() and height() of the containing widget.

Yaoming
26th February 2014, 12:19
Thanks for your answer, I'll work on that.
Could you explain to me how this all thing works please? I hate working on something I don't fully understand... When is the paintRequested signal emitted?

ChrisW67
26th February 2014, 20:44
The preview dialog emits paintRequested() when it needs to draw the previewed content. It does this at once immediately after the dialog is shown, but it may emit the signal again if the user does things like select another printer, page size, or margins using the controls in the dialog. Each time the signal is emitted the preview area starts empty and you should render the entire document onto the supplied printer.

Yaoming
28th February 2014, 12:55
Thanks for your help. I'm using the print function instead of my painter and it works just fine.