def export_to_pdf(self: 'BaseWidget', filename: Path): width_a4 = 730 height_a4 = 1060 is_landscape = self.width() > self.height() if is_landscape: self.resize(height_a4, width_a4) # landscape else: self.resize(width_a4, height_a4) # portrait # render widget to picture picture = QPicture() painter = QPainter(picture) self.render(painter, QPoint(0, 0)) painter.end() # set up PDF printer printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(str(filename)) if is_landscape: printer.setOrientation(QPrinter.Landscape) # draw picture on printer painter = QPainter() ok = painter.begin(printer) if ok: painter.drawPicture(0, 0, picture) ok = painter.end() return ok
def generatePlotFormat(plot, width, height): picture = QPicture() qcpPainter = QCPPainter() qcpPainter.begin(picture) plot.toPainter(qcpPainter, width, height) qcpPainter.end() result = QTextCharFormat() result.setObjectType(MainWindow.QCPTextFormat) result.setProperty(MainWindow.QCPData, picture) return result
def testFromData(self): picture = QPicture() painter = QPainter() painter.begin(picture) painter.drawEllipse(10, 20, 80, 70) painter.end() data = picture.data() picture2 = QPicture() picture2.setData(data) self.assertEqual(picture2.data(), picture.data()) w = MyWidget() w._picture = picture2 w._app = self.app QTimer.singleShot(300, w.show) self.app.exec_()