如何在 QChartview 中调整太小的图表大小通过 QTDesigner 设计的 GUI

问题描述

我在 QTDesigner 中设计了一个 GUI,我想在其中包含一个使用 PyQT5 的 QChart 的甜甜圈图。我已经实现了让图表本身可见并隐藏图例。但是,我不知道如何使图表适合定义的 Chartview 区域的整个区域。如图所示,它真的很小。

enter image description here

以下是我的代码的相关片段:

from PyQt5 import QtCore,QtWidgets
from PyQt5.QtChart import QChart,QChartView,QPieSeries
from PyQt5.QtGui import QColor
import csv
from operator import itemgetter


class Ui_MainWindow(object):
    def setupUi(self,MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1118,702)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(0,20,1091,611))
        self.tabWidget.setObjectName("tabWidget")
    
    # removed a lot of other graphical elements for readability
        
        self.t_type_pressure = QtWidgets.QWidget()              #create a tab which contains a group box
        self.t_type_pressure.setObjectName("t_type_pressure")
        self.groupBox = QtWidgets.QGroupBox(self.t_type_pressure)
        self.groupBox.setGeometry(QtCore.QRect(20,60,511,311))
        self.groupBox.setObjectName("groupBox")
        self.gv_all_types = QChartView(self.groupBox)           #add a QChartView to the group box
        self.gv_all_types.setGeometry(QtCore.QRect(0,100,150)) #set the size of the QChartView container which holds the chart
        self.gv_all_types.setObjectName("gv_all_types")
        

        
    def retranslateUi(self,MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow","MainWindow"))
        # removed the connections between buttons and functions,one of them called the connectCSVDatabase

    
    def connectCSVDatabase(self):
        # do connection to database here and get data

        # ... get data from CSV file

        # initialize all data containers in GUI
        self.series_all_types = QPieSeries()
        self.series_all_types.setHoleSize(0.45)
        self.series_all_types.setPieSize(1)
        self.series_all_types.append('A',1).setColor(QColor(0,148,255))
        self.series_all_types.append('B',1).setColor(QColor(255,144,0))
        self.series_all_types.append('C',8,0))
        self.series_all_types.hide()


        self.chart_all_types = self.gv_all_types.chart()
        self.chart_all_types.addSeries(self.series_all_types)
        self.chart_all_types.setBackgroundRoundness(0)
        self.chart_all_types.layout().setContentsMargins(1,1,1)
        
        self.update_shown_data()

    def update_shown_data(self):
        # clear the existing series and add the new ones
        self.series_all_types.clear()
        self.series_all_types.append('A',10).setColor(QColor(0,20).setColor(QColor(255,30).setColor(QColor(255,0))

        self.chart_all_types.addSeries(self.series_all_types)

使用上面的代码,我的容器在 GUI 中清晰可见,但图表本身非常小(请参阅附带的图片)。希望有人能帮帮我!

解决方法

在创建图表后在代码中添加以下行后终于解决了这个问题:

self.chart_all_alarms.setPlotArea(QtCore.QRectF(0,100,130))

我在 Qt 文档中发现了以下内容:The plot area does not include the area defined by margins. By default this will resize if inside a QChartView. If an explicit size is set for the plot area then it will respect this

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...