How to change Label size of QPieSlice in QtChart

983 Views Asked by At

I tried using the slice.setLabelFont(font) but its not working, I have attached my code below

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        #Load the UI Page
        THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
        my_file = os.path.join(THIS_FOLDER, 'main.ui')
        uic.loadUi(my_file, self)

        # changing the background color to cyan
        self.setStyleSheet("background-color: #363636;")
  
        # set the title
        self.setWindowTitle("Screen Time")

        # self.graphWidget = pg.PlotWidget()
        # self.setCentralWidget(self.graphWidget)

        font=QtGui.QFont()
        font.setPixelSize(20)
        font.setPointSize(20)
        

        series = QtChart.QPieSeries()
        series.setHoleSize(0.5)
        series.append("Example1 40%",40.0)
        
      
        slice = QtChart.QPieSlice()

        slice.setLabelFont(font)

        slice = series.append("Example1 30%",30.0)
        slice.setLabelVisible()
        
        series.append("Example1 30%",30.0)

        chart = QtChart.QChart()
        
        chart.addSeries(series)
        chart.setTitleFont(font)
        chart.setTitle('usage time')
        
        chart.setAnimationOptions(QtChart.QChart.SeriesAnimations)
        chart.setTheme(QtChart.QChart.ChartThemeDark)
        #chart.animation

        chart.setBackgroundBrush(QtGui.QBrush(QtGui.QColor("transparent")))
        chart.legend().setVisible(True)
        chartview = QtChart.QChartView(chart)
        chartview.setRenderHint(QtGui.QPainter.Antialiasing)

        self.chart_container.setStyleSheet("background-color: #363636;")

        self.chart_container.setContentsMargins(0, 0, 0, 0)
        lay = QtWidgets.QHBoxLayout(self.chart_container)
        lay.setContentsMargins(0, 0, 0, 0)
        lay.addWidget(chartview)

enter image description here

1

There are 1 best solutions below

0
alec On

There are two problems. First, you are calling setLabelFont on a QPieSlice that never gets added to the QPieSeries.

slice = QtChart.QPieSlice() # this is never added to the series
slice.setLabelFont(font)

slice = series.append("Example1 30%",30.0) # creates and returns a new pie slice
slice.setLabelVisible()

And second, setting the chart theme to QChart.ChartThemeDark will change the font back to the default, so it must be set before the QPieSlice label font. As mentioned in the docs:

Changing the theme will overwrite all customizations previously applied to the series.

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        #Load the UI Page
        THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
        my_file = os.path.join(THIS_FOLDER, 'main.ui')
        uic.loadUi(my_file, self)
        self.setStyleSheet("background-color: #363636;")
        self.setWindowTitle("Screen Time")

        chart = QtChart.QChart()
        chart.setTheme(QtChart.QChart.ChartThemeDark)

        font=QtGui.QFont()
        font.setPixelSize(20)
        font.setPointSize(20)
        
        series = QtChart.QPieSeries()
        series.setHoleSize(0.5)
        series.append("Example1 40%",40.0)
        
        slice = series.append("Example1 30%",30.0)
        slice.setLabelVisible()
        slice.setLabelFont(font)
        
        series.append("Example1 30%",30.0)
        chart.addSeries(series)
        
        chart.setTitleFont(font)
        chart.setTitle('usage time')
        chart.setAnimationOptions(QtChart.QChart.SeriesAnimations)
        chart.setBackgroundBrush(QtGui.QBrush(QtGui.QColor("transparent")))
        chart.legend().setVisible(True)
        chartview = QtChart.QChartView(chart)
        chartview.setRenderHint(QtGui.QPainter.Antialiasing)

        self.chart_container.setStyleSheet("background-color: #363636;")

        self.chart_container.setContentsMargins(0, 0, 0, 0)
        lay = QtWidgets.QHBoxLayout(self.chart_container)
        lay.setContentsMargins(0, 0, 0, 0)
        lay.addWidget(chartview)