QPrinter and two separate widgets to print on one page
I have two separate widgets. I made a QPrintDialog and connected its paintRequested signal to my own slots of both widgets. The slots are here:
Code:
void PicWidget
::Print( QPrinter* printer
) {
cout << "PCW:P"<<endl;
QPainter* painter
= printer
->paintEngine
()->painter
();
if(!painter)
painter->begin(printer);
painter->drawText(100,100, "PICW");
painter->end();
}
void BrWidget
::Print( QPrinter* printer
) {
QPainter* painter
= printer
->paintEngine
()->painter
();
if(!painter)
painter->begin(printer);
painter->drawText(100,200, "BRW");
painter->end();
}
But it doesn't work: only one of the methods works properly. It also writes that two QPainters are working at once. How can i solve the problem?
Re: QPrinter and two separate widgets to print on one page
I would remove the lines with checking the paint engine. Also you shouldn't connect that signal to more than one slot at once. Instead connect the signal to a custom slot not in the widgets but in the object that controls the printing (like the parent widget of the two widgets).
But I would also use this code:
Code:
MyWidget1 *w1 = ...
MyWidget2 *w2 = ...
// setupPrinter, or start here if you're triggered on paintRequested()
// modify (scale, translate) painter if needed
w1->render(&painter);
// modify (scale, translate) painter if needed
w2->render(&painter);