Results 1 to 6 of 6

Thread: Bug in Qt5.12 or PyQt5.11.3???

  1. #1
    Join Date
    Jan 2019
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Bug in Qt5.12 or PyQt5.11.3???

    Hey there!

    I have a problem with PyQt5 and QPrinter which seems to be related to PyQt 5.11.3 or Qt 5.12. I’m running Manjaro Linux (Kenel 4.20) with python 3.7.2, pyqt5-common 5.11.3, python-pyqt5 5.11.3 and Qt5 at v5.12.0.
    (The problem occurs also in openSuse Tumbleweed. It seems, that it is not specific to Manjaro or Arch Linux)

    I have a program which printed directly to a pdf-file in portrait- or landscape-orientation using QPrinter. Everything worked perfectly with PyQt5 v5.11.2 and Qt5 v5.11.2.
    But now after update to Qt5.12 and pyqt5.11.3, whitout any code-changes, I always get documents in format "us-letter" in portrait-orientation.

    I have a minimal working-example and some pdf-files to illustrate this here: https://www.dropbox.com/sh/feve6ubvy...5zGdQI9ra?dl=0
    (I had to remove names in two of the documents with LibreOffice Draw. Thus, the specs say that they are generated by LibreOffice instead of Qt5.12.)

    In my example script, I let python print the printer-settings and they are correct, but the generated output is wrong.

    Can anybody confirm and reproduce this behaviour with my working example?

    Thank you!

    Regards,

    Sam Barkowski

    Here is a working example in a Code-Tag:

    Qt Code:
    1. from decimal import Decimal
    2. import os
    3. import sys
    4. import platform
    5. import tempfile
    6. from PyQt5.QtWidgets import QWidget, QToolTip, QPushButton, QApplication
    7. from PyQt5.QtGui import QPainter, QFont, QDesktopServices, QPen, QColor, QTextOption, QPageLayout, QPageSize
    8. from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
    9. from PyQt5.QtCore import Qt, QUrl, QRectF, QMarginsF, QSizeF
    10.  
    11. class Example(QWidget):
    12.  
    13. def __init__(self):
    14. super().__init__()
    15. self.initUI()
    16. self.printer = QPrinter(300) # QPrinter.HighResolution
    17. self.printer.setPageSize(QPrinter.A4)
    18. self.printer.setOutputFormat(QPrinter.PdfFormat)
    19. print("Printercheck1:", self.printer.isValid())
    20. self.tempdir = tempfile.mkdtemp(prefix="PyTesting-")
    21. self._Windows = False
    22. if platform.system() == "Windows":
    23. self._Windows = True
    24.  
    25.  
    26. def initUI(self):
    27.  
    28. QToolTip.setFont(QFont('SansSerif', 10))
    29.  
    30. self.setToolTip('Try printing')
    31.  
    32. btn = QPushButton('Print portrait-pdf', self)
    33. btn.setToolTip('This should produce a PDF in portrait mode in A4 format')
    34. btn.resize(btn.sizeHint())
    35. btn.move(60, 50)
    36. btn.clicked.connect(lambda:self.exPrint(pdf=True, lscp=False))
    37.  
    38. btn2 = QPushButton('Print landscape-pdf', self)
    39. btn2.setToolTip('This should produce a PDF in landscape mode in A4 format')
    40. btn2.resize(btn.sizeHint())
    41. btn2.move(60, 100)
    42. btn2.clicked.connect(lambda:self.exPrint(pdf=True, lscp=True))
    43.  
    44. btn3 = QPushButton('Print dialog portrait', self)
    45. btn3.setToolTip('Open Print-Dialog portrait')
    46. btn3.resize(btn.sizeHint())
    47. btn3.move(60, 150)
    48. btn3.clicked.connect(lambda:self.exPrint(pdf=False, lscp=False))
    49.  
    50. btn4 = QPushButton('Print dialog lscp', self)
    51. btn4.setToolTip('Open Print-Dialog landscape')
    52. btn4.resize(btn.sizeHint())
    53. btn4.move(60, 200)
    54. btn4.clicked.connect(lambda:self.exPrint(pdf=False, lscp=True))
    55.  
    56. self.setGeometry(400, 400, 400, 300)
    57. self.setWindowTitle('Printing-Test')
    58. self.show()
    59.  
    60. def printsettings(self, pdf=True, name="", landscp=False):
    61. printservice = None
    62. painter = None
    63. filename = None
    64. if pdf or self._Windows:
    65. printservice = self.printer
    66. if landscp:
    67. try:
    68. printservice.setPageOrientation(QPageLayout.Landscape)
    69. print("Printercheck2:", printservice.isValid())
    70. except Exception as e:
    71. print("Exception in printersettings:", e.args[0])
    72. else:
    73. try:
    74. printservice.setPageOrientation(QPageLayout.Portrait)
    75. except Exception as e:
    76. print("Exception in printersettings::", e.args[0])
    77. print("Current settings:", printservice.pageLayout().pageSize().size(QPageSize.Millimeter), printservice.pageLayout().orientation())
    78. if self._Windows:
    79. slash = "\\"
    80. else:
    81. slash = "/"
    82. filename = self.tempdir + slash + name + ".pdf"
    83. self.printer.setOutputFileName(filename)
    84. self.printer.setDocName(filename)
    85. else:
    86. filename = ""
    87. printservice = QPrinter(300) # 300 QPrinter.HighResolution
    88. printservice.setFullPage(True)
    89. printservice.setPageSize(QPrinter.A4)
    90. if landscp:
    91. try:
    92. printservice.setPageOrientation(QPageLayout.Landscape)
    93. except Exception as e:
    94. print("Exception in printersettings:", e.args[0])
    95. else:
    96. try:
    97. printservice.setPageOrientation(QPageLayout.Portrait)
    98. except Exception as e:
    99. print("Exception in printersettings:", e.args[0])
    100. dialog = QPrintDialog(printservice, None)
    101. if not dialog.exec_():
    102. return (None, None, None)
    103. if printservice is not None:
    104. painter = QPainter(printservice)
    105. painter.setRenderHints(QPainter.TextAntialiasing)
    106. painter.resetTransform()
    107. scale = Decimal(1)
    108. painter.scale(scale, scale)
    109. return (printservice, painter, filename)
    110.  
    111. def finishjob(self, painter, filename, pdf=True):
    112. painter.end()
    113. if pdf:
    114. url = QUrl.fromLocalFile(filename)
    115. QDesktopServices.openUrl(url)
    116. else:
    117. if self._Windows:
    118. try:
    119. os.startfile(filename, "print")
    120. except Exception as e:
    121. print("Exception in attempt to print file:", filename, "ErrorMessage:", e.args[0])
    122.  
    123.  
    124. def mm2pt(self, x):
    125. """used to calculate millimeters to point, for passing lengths in millimeter to the painter"""
    126. if type(x) == float or type(x) == int:
    127. return int(round(Decimal("360")/Decimal("127")*Decimal(x)))
    128. else:
    129. return None
    130.  
    131. def fromLeft(self, x):
    132. """distance from left paper edge in millimeters"""
    133. return (self.mm2pt(x) - 18)
    134.  
    135. def fromTop(self, y):
    136. """distance from top edge of paper in millimeters"""
    137. return (self.mm2pt(y) - 8)
    138.  
    139. def exPrint(self, pdf=True, lscp=False):
    140. name = 'printingexample'
    141. printmode = self.printsettings(pdf, name, lscp)
    142. printservice = printmode[0]
    143. painter = printmode[1]
    144. filename = printmode[2]
    145. if printservice is not None:
    146. pen = QPen(QColor(0, 0, 0))
    147. pen.setWidthF(0.4)
    148. painter.setPen(pen)
    149. sansFont = QFont("Sans Serif", 9, weight=45)
    150. painter.setFont(sansFont)
    151. x1 = self.fromLeft(17)
    152. x2 = x1 + self.mm2pt(200)
    153. t_left = QTextOption()
    154. t_left.setAlignment(Qt.AlignLeft)
    155. t_right = QTextOption()
    156. t_right.setAlignment(Qt.AlignRight)
    157. height = 12
    158. rect = QRectF(x1, self.mm2pt(10), x2 - x1, 2*height)
    159. painter.drawText(rect, "top left", t_left)
    160. painter.drawText(rect, "top right", t_right)
    161. try:
    162. self.finishjob(painter, filename, pdf)
    163. except (PermissionError, ProcessLookupError, FileExistsError, FileNotFoundError,
    164. AttributeError, AssertionError, NotADirectoryError) as e:
    165. print("Failed to open or print document. ErrorMessage:", e.args[0])
    166. else:
    167. print("Printservice not available.")
    168.  
    169. def main():
    170. app = QApplication(sys.argv)
    171. ex = Example()
    172. sys.exit(app.exec_())
    173.  
    174. if __name__ == "__main__":
    175. main()
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bug in Qt5.12 or PyQt5.11.3???

    Between the two libraries I would rather assume the responsibility in Qt than in the Python Wrapper.

    Have you checked the printer's pageSize at the time of printing? Is it still set to A4 set by you in the constructor but not using it when generating?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2019
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Bug in Qt5.12 or PyQt5.11.3???

    Hey anda_skoa,

    thanks for your answer.

    Yes, the pageSize is still set to A4.
    This is also true when I try to print via printdialog (and not directly into a pdf-file). The settings are passed correctly, but I get wrong output with my HP printer and also when I print to pdf with the installed pdfprinter of my system.

    I've also tried this with pySide2 (i. e. Qt for Python) and the result is the same.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bug in Qt5.12 or PyQt5.11.3???

    That sounds very much like a bug indeed.

    Is it just wrong with A4 or does it always end using Letter even when you use any other different size?

    Given that you experience this with portrait but not landscape, have you tried setting page size after page orientation?

    You could also try QPdfWriter but I guess it will have the same problem (very likely sharing most of the implementation).

    Cheers,
    _

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

    barkowski@shinobi-mail.de (26th January 2019)

  6. #5
    Join Date
    Jan 2019
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Bug in Qt5.12 or PyQt5.11.3???

    The output is always in portrait and us-letter (setting landscape has no effect) and the wrong page dimensions are independent of the desired page orientation.
    It is not depending on A4, too. Same result with othe page dimensions as A5 or A3.

    Should I place a bug-report here: https://bugreports.qt.io/secure/Dashboard.jspa?

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bug in Qt5.12 or PyQt5.11.3???

    Quote Originally Posted by barkowski@shinobi-mail.de View Post
    The output is always in portrait and us-letter (setting landscape has no effect
    Oh, wow, even worse than how I interpreted it.

    Quote Originally Posted by barkowski@shinobi-mail.de View Post
    Should I place a bug-report here: https://bugreports.qt.io/secure/Dashboard.jspa?
    Yes, definitely.

    I looked a bit and couldn't find a simlar report.

    Ideally attach a minimal test program, e.g. using Qt for Python, that shows the problem.

    Cheers,
    _

Similar Threads

  1. PyQt5 Questions
    By Miton in forum Newbie
    Replies: 6
    Last Post: 30th January 2017, 13:10
  2. PyQt5 tutorials
    By riverbee in forum General Discussion
    Replies: 0
    Last Post: 5th August 2016, 02:42
  3. PyQt5 chart
    By ecce in forum Newbie
    Replies: 2
    Last Post: 24th April 2016, 15:06
  4. How can I install pyQt5
    By prachi kamble in forum Installation and Deployment
    Replies: 0
    Last Post: 4th June 2015, 13:13
  5. PyQt5 QPixmap
    By ChrisOfBristol in forum Newbie
    Replies: 4
    Last Post: 4th April 2015, 22:48

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.