PDA

View Full Version : QPrinter and two separate widgets to print on one page



YaK
5th July 2009, 09:46
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:



void PicWidget::Print( QPrinter* printer )
{
cout << "PCW:P"<<endl;
QPainter* painter = printer->paintEngine()->painter();
if(!painter)
painter = new QPainter(printer);

painter->begin(printer);
painter->drawText(100,100, "PICW");
painter->end();
}

void BrWidget::Print( QPrinter* printer )
{
QPainter* painter = printer->paintEngine()->painter();
if(!painter)
painter = new QPainter(printer);


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?

wysota
5th July 2009, 10:10
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:

MyWidget1 *w1 = ...
MyWidget2 *w2 = ...

QPrinter printer;
// setupPrinter, or start here if you're triggered on paintRequested()
QPainter painter(&printer);
// modify (scale, translate) painter if needed
w1->render(&painter);
// modify (scale, translate) painter if needed
w2->render(&painter);