Results 1 to 2 of 2

Thread: QPrinter and two separate widgets to print on one page

  1. #1
    Join Date
    Nov 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. void PicWidget::Print( QPrinter* printer )
    2. {
    3. cout << "PCW:P"<<endl;
    4. QPainter* painter = printer->paintEngine()->painter();
    5. if(!painter)
    6. painter = new QPainter(printer);
    7.  
    8. painter->begin(printer);
    9. painter->drawText(100,100, "PICW");
    10. painter->end();
    11. }
    12.  
    13. void BrWidget::Print( QPrinter* printer )
    14. {
    15. QPainter* painter = printer->paintEngine()->painter();
    16. if(!painter)
    17. painter = new QPainter(printer);
    18.  
    19.  
    20. painter->begin(printer);
    21. painter->drawText(100,200, "BRW");
    22. painter->end();
    23. }
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by wysota; 5th July 2009 at 10:05. Reason: Changed [qtclass] to [code]

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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:
    Qt Code:
    1. MyWidget1 *w1 = ...
    2. MyWidget2 *w2 = ...
    3.  
    4. QPrinter printer;
    5. // setupPrinter, or start here if you're triggered on paintRequested()
    6. QPainter painter(&printer);
    7. // modify (scale, translate) painter if needed
    8. w1->render(&painter);
    9. // modify (scale, translate) painter if needed
    10. w2->render(&painter);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.