【问题标题】:How to change Label size of QPieSlice in QtChart如何在 QtChart 中更改 QPieSlice 的标签大小
【发布时间】:2021-07-25 01:01:53
【问题描述】:

我尝试使用 slice.setLabelFont(font) 但它不起作用, 我在下面附上了我的代码

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)

【问题讨论】:

    标签: python python-3.x pyqt pyqt5 pyqtchart


    【解决方案1】:

    有两个问题。首先,您在从未添加到 QPieSeries 的 QPieSlice 上调用 setLabelFont

    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()
    

    其次,将图表主题设置为QChart.ChartThemeDark会将字体改回默认值,因此必须在QPieSlice标签字体之前设置。正如docs中提到的:

    更改主题将覆盖之前应用于该系列的所有自定义设置。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      相关资源
      最近更新 更多