python - 如何在 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, 20, 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', 1).setColor(QColor(255,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, 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,148,255))
self.series_all_types.append('B', 20).setColor(QColor(255,144,0))
self.series_all_types.append('C', 30).setColor(QColor(255,8,0))
self.chart_all_types.addSeries(self.series_all_types)
使用上面的代码,我的容器在 GUI 中清晰可见,但图表本身非常小(参见包含的图片)。我希望有人可以帮助我!
解决方案
在创建图表后在代码中添加以下行后终于解决了这个问题:
self.chart_all_alarms.setPlotArea(QtCore.QRectF(0,0,100,130))
我在 Qt 文档中找到了以下内容:绘图区域不包括边距定义的区域。默认情况下,如果在 QChartView 内,它将调整大小。如果为绘图区域设置了明确的大小,那么它将尊重这一点
推荐阅读
- python - 如何仅使用特定域名限制登录 google api?
- r - 在数据框中按因子应用 approxfun
- python - Python Regex 编译拆分字符串,以便单词首先出现
- web-hosting - 短期网络托管
- sumo - 如何在win8上的SUMO中使用randomTrips.py
- gradle - 将 gradle 属性文件指定为 gradlew 的命令行参数
- javascript - 状态更改导致侧边栏重新加载
- c++ - 有没有更有效的方法来替换这些多个 IF 语句?
- react-native - 如何在 React Native 的 createStackNavigator 中创建 createDrawerNavigator?
- dns - 挖掘DNS,有时会得到正确的答案,有时会得到错误的答案