Results 1 to 2 of 2

Thread: Draw on QPrinter: color gets lost

  1. #1
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Draw on QPrinter: color gets lost

    Hi,

    I want to print something. That's not a big problem: I create a QPrinter and a QPainter to paint on the QPrinter. I can draw an image and some text. I also want to draw some lines. In the preview window (QPrintPreviewDialog), everything looks fine. But, when I print to PDF with an external PDF-Printer or to a real printer, the lines lose their color and appear in black. The Textcolor remains at it should be...

    Qt Code:
    1. void MyClass::Print(bool preview)
    2. {
    3. QPrinter *printer = new QPrinter(QPrinter::HighResolution;
    4. printer->setPageMargins(20,20,20,20,QPrinter::DevicePixel);
    5. printer->setColorMode(QPrinter::Color);
    6. printer->setPageSize(QPrinter::A4);
    7. printer->setPrintRange(QPrinter::AllPages);
    8.  
    9. if(preview)
    10. {
    11. QPrintPreviewDialog ppd(printer);
    12. connect(&ppd, SIGNAL(paintRequested(QPrinter*)), this, SLOT(Print(QPrinter*)));
    13. ppd.exec();
    14. }
    15. else
    16. {
    17. QPrintDialog *printDialog = new QPrintDialog(printer, CMainFrame::GetMainFrame().GetMainWin());
    18. if (printDialog->exec())
    19. Print(printer);
    20. }
    21. delete printer;
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MyClass::Print(QPrinter* printer)
    2. {
    3. //this is just a demonstration. My "real" code makes more sense ;)
    4.  
    5. QPainter p(printer);
    6. QRectF border(QPoint(0,0), printer->pageRect().size() - QSize(1,1));
    7.  
    8. p.setPen(Qt::red);
    9. p.drawRect(border);
    10.  
    11. QDateTime time = QDateTime::currentDateTime();
    12. QString timestr = time.toString("dddd, MMMM d, yyyy hh:mm:ss");
    13. p.drawText(400, 400, timestr);
    14.  
    15. p.drawLine(QLine(1000, 2000, 1800, 3500));
    16. }
    To copy to clipboard, switch view to plain text mode 

    see the attached results. Preview: preview.png, Print:print.png

    what can I do to print colored lines? I could draw everything onto a QPixmap first and
    print this QPixmap. But I think this may be too much memory-consuming, especially when printing in HighResolution mode...

    thanks!
    Felix

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Draw on QPrinter: color gets lost

    I solved it! I have to create a pen and set the width explicitly. weird...

    Qt Code:
    1. QPen pen(Qt::red);
    2. pen.setWidth(1);
    3. p.setPen(pen);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Draw line with inverted background color
    By viswanath in forum Qt Programming
    Replies: 4
    Last Post: 12th November 2011, 03:57
  2. How to draw QRubberBand with red color
    By lni in forum Qt Programming
    Replies: 15
    Last Post: 10th March 2010, 09:49
  3. Draw an 1-bit mask in color?
    By stephenju in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2010, 15:51
  4. Replies: 12
    Last Post: 25th December 2009, 15:07
  5. How to draw a diagram in a QPrinter?
    By nifei in forum Qt Programming
    Replies: 5
    Last Post: 24th February 2009, 03:41

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.