follow_the_sun
follow_the_sun

Reputation: 1

Save diagram as image in PyQt5

I'm trying to save my diagram on a PyQt5 widget as a picture (.png or .jpg) but not finding ways to do that. Code for creating diagram:

self.clearLayout(self.diagram_up)
    self.series_ = QPieSeries()
    self.series_.setHoleSize(0.35)
    self.slice_ = QPieSlice()
    self.slice_.setExploded()
    self.slice_.setLabelVisible()
    for slice in self.series_.slices():
        slice.setLabel("<h3>{:.2f}%</h3>".format(100 * slice.percentage()))
    self.chart_ = QChart()
    self.chart_.legend().hide()
    self.chart_.addSeries(self.series_)
    self.chart_.setAnimationOptions(QChart.SeriesAnimations)
    self.chart_.setTitle("<span style='color: black; font-size: 18pt;'>Статистика по операциям</span>")
    self.chartview_ = QChartView(self.chart_)
    self.chartview_.setRenderHint(QPainter.Antialiasing)
    self.diagram_up.addWidget(self.chartview_)

Upvotes: 0

Views: 713

Answers (1)

eyllanesc
eyllanesc

Reputation: 244003

QChartView is a QGraphicsView so you can use the render method.

pixmap = QPixmap(self.chartview_.sceneRect().size().toSize())
pixmap.fill(QColor("transparent"))

painter = QPainter(pixmap)
painter.setRenderHint(QPainter.Antialiasing)

self.chartview_.render(painter)
painter.end()

pixmap.save("image.png")

Upvotes: 1

Related Questions