Results 1 to 6 of 6

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

Similar Threads

  1. PyQt5 Questions
    By Miton in forum Newbie
    Replies: 6
    Last Post: 30th January 2017, 12:10
  2. PyQt5 tutorials
    By riverbee in forum General Discussion
    Replies: 0
    Last Post: 5th August 2016, 01:42
  3. PyQt5 chart
    By ecce in forum Newbie
    Replies: 2
    Last Post: 24th April 2016, 14:06
  4. How can I install pyQt5
    By prachi kamble in forum Installation and Deployment
    Replies: 0
    Last Post: 4th June 2015, 12:13
  5. PyQt5 QPixmap
    By ChrisOfBristol in forum Newbie
    Replies: 4
    Last Post: 4th April 2015, 21: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.