QtCharts in PyQt5 widget container

67 views Asked by At

I have an issue with inserting a QtChart into a window I've created with Qt Designer. The window consists of a few buttons and a widget container. In this container I want to add a chart from the QtChart library. In the code below the chart does appear in the window, but it takes up the whole window.

How do I display the chart inside of a widget container?

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.showMaximized()
        self.ui.newVerification_pushButton.clicked.connect(self.runNewVerification)
        self.ui.verificationList_pushButton.clicked.connect(self.runGeneralLedgerViewer)
        self.ui.listOfAccounts_pushButton.clicked.connect(self.runListOfAccountsWindow)
        self.create_piechart() #<--- Calling the function below.



    def create_piechart(self):
        series = QPieSeries()
        series.append("Python", 80)
        series.append("C++", 70)
        series.append("Java", 50)


        chart = QChart()
        chart.addSeries(series)
        chart.setAnimationOptions(QChart.SeriesAnimations)
        chart.setTitle("Programming pie chart")

        chartview = QChartView(chart)
        chartview.setRenderHint(QPainter.Antialiasing)
        self.setCentralWidget(chartview) #<--- Makes the chart take up the whole window.

I tried this line self.ui.AssetChart_widget.setLayout(chartview) instead of the last line in the block of code above, but it results in an error saying: TypeError: setLayout(self, a0: QLayout): argument 1 has unexpected type 'QChartView'.

What am I missing/doing wrong? Or is this a simple matter of that "it's not possible to add a chart to a container in a window"?!

0

There are 0 answers