如何解决如何在 QChartview 中调整太小的图表大小通过 QTDesigner 设计的 GUI
我在 QTDesigner 中设计了一个 GUI,我想在其中包含一个使用 PyQT5 的 QChart 的甜甜圈图。我已经实现了让图表本身可见并隐藏图例。但是,我不知道如何使图表适合定义的 Chartview 区域的整个区域。如图所示,它真的很小。
以下是我的代码的相关片段:
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))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。