Results 1 to 8 of 8

Thread: Printing with a coordinate system given in millimeters

  1. #1
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Printing with a coordinate system given in millimeters

    I was wondering what the best (simple and efficient) way would to for printing in Qt when the given coordinate system is defined in absolute coordinates given in millimeters.
    To better explain the problem: I have a definition of what needs to printed specified by different "tokens" (point, line, rectangle, text, etc.) with coordinates (and text sizes) given in millimeters. The positions are relative to the top left corner of a standard A4 page.

  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: Printing with a coordinate system given in millimeters

    Use QPainter::setWindow() and QPainter::setViewport() to inform Qt about logical and physical coordinates respectively. You can also use GraphicsView with a scene that has dimensions reflecting the logical coordinates that you want to have.
    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.


  3. The following 2 users say thank you to wysota for this useful post:

    doberkofler (25th April 2010), Toniy (5th April 2016)

  4. #3
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Printing with a coordinate system given in millimeters

    Thank you for the hint. It works just fine except when settings the font size where I'm stuck.
    I the following code example the coordinate translation works fine except for the font size that needs to either set in pixel or point.

    Qt Code:
    1. class Report : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Report(const QString& theTitle, int theNumberOfPages) : itsTitle(theTitle), itsNumberOfPages(theNumberOfPages) {}
    7. void preview();
    8. void printPage(QPainter* thePainter, int thePage);
    9.  
    10. private slots:
    11. void printDocument(QPrinter* thePrinter);
    12.  
    13. private:
    14. QString itsTitle;
    15. int itsNumberOfPages;
    16. };
    17.  
    18. void Report::preview()
    19. {
    20. QPrinter aPrinter(QPrinter::HighResolution);
    21. QPrintPreviewDialog aPreview(&aPrinter, 0);
    22. connect(&aPreview, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printDocument(QPrinter *)));
    23. aPreview.exec();
    24. }
    25.  
    26. void Report::printDocument(QPrinter* thePrinter)
    27. {
    28. thePrinter->setFromTo(1, itsNumberOfPages);
    29.  
    30. QString aTitle = QString(tr("Preparing %1")).arg(itsTitle);
    31. QProgressDialog aProgress(aTitle, tr("&Cancel"), 0, itsNumberOfPages, 0);
    32. aProgress.setWindowModality(Qt::ApplicationModal);
    33. aProgress.setWindowTitle(itsTitle);
    34. aProgress.setMinimum(thePrinter->fromPage() - 1);
    35. aProgress.setMaximum(thePrinter->toPage());
    36.  
    37. // Configure a painter with local coordinate system in 1/10th of a millimeter based on an A4 landscape page
    38. QPainter aPainter;
    39. aPainter.begin(thePrinter);
    40. int aLogicalWidth = 21000;
    41. int aLogicalHeight = 29700;
    42. aPainter.setWindow(0, 0, aLogicalWidth, aLogicalHeight);
    43. int aPhysicalWidth = thePrinter->width();
    44. int aPhysicalHeight = thePrinter->height();
    45. aPainter.setViewport(0, 0, aPhysicalWidth, aPhysicalHeight);
    46.  
    47. // Print all pages
    48. bool aFirstPage = true;
    49. for (int aPage = thePrinter->fromPage(); aPage <= thePrinter->toPage(); ++aPage)
    50. {
    51. if (!aFirstPage)
    52. thePrinter->newPage();
    53.  
    54. qApp->processEvents();
    55. if (aProgress.wasCanceled())
    56. break;
    57.  
    58. // Print page
    59. printPage(&aPainter, aPage);
    60.  
    61. aProgress.setValue(aPage);
    62. aFirstPage = false;
    63. }
    64.  
    65. aPainter.end();
    66. }
    67.  
    68. void Report::printPage(QPainter* thePainter, int thePage)
    69. {
    70. // Set pen
    71. QPen aPen;
    72. aPen.setColor(Qt::black);
    73. aPen.setWidth(0);
    74. aPen.setStyle(Qt::SolidLine);
    75. thePainter->setPen(aPen);
    76.  
    77. // Set brush
    78. QBrush aBrush;
    79. aBrush.setColor(Qt::black);
    80. aBrush.setStyle(Qt::SolidPattern);
    81. thePainter->setBrush(aBrush);
    82.  
    83. // Set font
    84. QFont aFont;
    85. aFont.setFamily("Arial");
    86. aFont.setPixelSize(100); // ??? how to set the font size to 1cm ???
    87. aFont.setWeight(QFont::Normal);
    88. aFont.setItalic(false);
    89. thePainter->setFont(aFont);
    90.  
    91. // Draw line
    92. thePainter->drawLine(QPoint(1000, 1000), QPoint(5000, 5000));
    93.  
    94. // Print a rectangle
    95. thePainter->drawRect(QRect(8000, 8000, 2000, 2000));
    96.  
    97. // Print a text
    98. QString aText = QString("%1 on page %2").arg(itsTitle).arg(thePage);
    99. thePainter->drawText(1000, 12000, aText);
    100. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to doberkofler for this useful post:

    Toniy (5th April 2016)

  6. #4
    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: Printing with a coordinate system given in millimeters

    If you scale the canvas in milimeters, the font size will also have to be given in milimeters but scaled with a factor matching the one between the window and the viewport (in other words you have to calculate how many "pixels" or "points" fit into one "milimeter").
    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.


  7. The following user says thank you to wysota for this useful post:

    doberkofler (25th April 2010)

  8. #5
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Printing with a coordinate system given in millimeters

    HI, I have a quite similar problem, I want to print a square 20 * 20 mm and I'm using this code:

    Qt Code:
    1. QPrinter printer;
    2. printer.setPaperSize(QPrinter::A4);
    3. printer.setOrientation (QPrinter::Portrait);
    4.  
    5. QPainter painter;
    6. int LogicalWidth = 210;
    7. int LogicalHeight = 297;
    8. painter.setWindow(0, 0,LogicalWidth, LogicalHeight);
    9. int PhysicalWidth = printer.width();
    10. int PhysicalHeight = printer.height();
    11. painter.setViewport(0, 0, PhysicalWidth, PhysicalHeight);
    12. painter.begin(&printer);
    13. painter.drawRect(50,50,20,20);
    14. painter.end();
    To copy to clipboard, switch view to plain text mode 

    the square I obtain is quite 5*5 mm , but i expect a square 20 * 20 mm...where is my mistake?
    Thankyou in advance.
    Last edited by wysota; 25th April 2010 at 19:42. Reason: missing [code] tags

  9. #6
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Printing with a coordinate system given in millimeters

    This is strange as your example looks correct to me. Maybe "wysota" has an idea.

  10. #7
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Printing with a coordinate system given in millimeters

    Thanks for reply.
    I resolved my problem in this way: taking in account printer resolution (hoping this is a general solution) :

    QTransform TPiXEL = QTransform::fromScale(
    painter.device()->physicalDpiX() / 25.400,
    painter.device()->physicalDpiY() / 25.400);
    painter.setWorldTransform(TPiXEL, false);

    Thanks.
    Last edited by _Jack_; 30th April 2010 at 15:36. Reason: updated contents

  11. #8
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Printing with a coordinate system given in millimeters

    Are you now using setWorldTransform together with setWindow/setViewport or instead of the method suggested by wysota?

Similar Threads

  1. coordinate system
    By Wojtek.wk in forum Newbie
    Replies: 7
    Last Post: 12th April 2010, 13:47
  2. 2D Graphics on coordinate system
    By soumya in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 06:27
  3. Replies: 1
    Last Post: 9th April 2009, 14:54
  4. The coordinate system
    By avis_phoenix in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2008, 12:16
  5. Replies: 22
    Last Post: 7th December 2007, 09:01

Tags for this Thread

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.