Insert QChartView to ui(将 QChartView 插入 ui)
问题描述
我试图把
<?xml version="1.0" encoding="UTF-8"?><ui版本="4.0"><class>主窗口</class><widget class="QMainWindow" name="MainWindow"><属性名称="几何"><正确><x>0</x><y>0</y><宽度>800</宽度><身高>600</身高></正确></属性><属性名="windowTitle"><字符串>主窗口</字符串></属性><widget class="QWidget" name="centralwidget"><widget class="QPushButton" name="pushButton"><属性名称="几何"><正确><x>210</x><y>280</y><宽度>88</宽度><身高>33</身高></正确></属性><属性名称="文本"><字符串>按钮</字符串></属性></小部件><widget class="QPushButton" name="pushButton_2"><属性名称="几何"><正确><x>340</x><y>80</y><宽度>88</宽度><身高>33</身高></正确></属性><属性名称="文本"><string>测试交易</string></属性></小部件><widget class="QWidget" name="chart_container" native="true"><属性名称="几何"><正确><x>29</x><y>29</y><宽度>291</宽度><身高>231</身高></正确></属性></小部件></小部件><widget class="QMenuBar" name="menubar"><属性名称="几何"><正确><x>0</x><y>0</y><宽度>800</宽度><身高>24</身高></正确></属性></小部件><widget class="QStatusBar" name="statusbar"/></小部件><资源/><连接/></ui>
然后通过布局将QChartView添加到容器中来实现代码:
导入系统从 PyQt5 导入 QtCore、QtWidgets、uic、QtChart# 加载两个ui文件uifile_1 = "UI/main.ui"form_1,base_1 = uic.loadUiType(uifile_1)类示例(base_1,form_1):def __init__(self):超级(base_1,自我).__init__()self.setupUi(self)数据 = ((1, 7380, 7520, 7380, 7510, 7324),(2, 7520, 7580, 7410, 7440, 7372),(3, 7440, 7650, 7310, 7520, 7434),(4, 7450, 7640, 7450, 7550, 7480),(5, 7510, 7590, 7460, 7490, 7502),(6, 7500, 7590, 7480, 7560, 7512),(7, 7560, 7830, 7540, 7800, 7584),)系列 = QtChart.QCandlestickSeries()series.setDecreasingColor(QtCore.Qt.red)series.setIncreasingColor(QtCore.Qt.green)ma5 = QtChart.QLineSeries() # 5天平均数据线tm = [] # 存储str类型数据# 在一个循环中,series 和 ma5 追加对应的数据对于数据中的 num、o、h、l、c、m:series.append(QtChart.QCandlestickSet(o, h, l, c))ma5.append(QtCore.QPointF(num, m))tm.append(str(num))图表 = QtChart.QChart()chart.addSeries(series) # 蜡烛图chart.addSeries(ma5) # ma5 线chart.setAnimationOptions(QtChart.QChart.SeriesAnimations)chart.createDefaultAxes()chart.legend().hide()chart.axisX(series).setCategories(tm)chart.axisX(ma5).setVisible(False)chartview = QtChart.QChartView(图表)self.chart_container.setContentsMargins(0, 0, 0, 0)放置 = QtWidgets.QHBoxLayout(self.chart_container)lay.setContentsMargins(0, 0, 0, 0)lay.addWidget(chartview)如果 __name__ == "__main__":应用程序 = QtWidgets.QApplication(sys.argv)ex = 示例()ex.show()sys.exit(app.exec_())
I am trying to put plot candlestick and 5-days average line on a same qtchart but give two x axis plot code into a UI loader
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
from PyQt5 import uic
import sys
from PyQt5.QtChart import QCandlestickSeries, QChart, QChartView, QCandlestickSet
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt, QPointF
from PyQt5 import QtChart as qc
# load both ui file
uifile_1 = 'UI/main.ui'
form_1, base_1 = uic.loadUiType(uifile_1)
uifile_2 = 'UI/main1.ui'
form_2, base_2 = uic.loadUiType(uifile_2)
class Example(base_1, form_1):
def __init__(self):
super(base_1, self).__init__()
self.setupUi(self)
self.pushButton_2.clicked.connect(self.change)
def change(self):
self.main = MainPage()
self.main.show()
# self.close()
class MainPage(base_2, form_2):
def __init__(self):
super(base_2, self).__init__()
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
I have tried putting a widget but have no idea what are promoted class name and header file for candlesticks.
The reason I want to do this is that I want to update this candlestick chart at a regular interval of time and also insert some buttons on that window.
UI
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>210</x>
<y>280</y>
<width>88</width>
<height>33</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>340</x>
<y>80</y>
<width>88</width>
<height>33</height>
</rect>
</property>
<property name="text">
<string>Test Trade</string>
</property>
</widget>
<widget class="PlotWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>140</x>
<y>100</y>
<width>120</width>
<height>80</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>PlotWidget</class>
<extends>QWidget</extends>
<header>qtgraph</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
It is not necessary to promote a class, although it is a valid option, so that it can be displayed in a window generated based on a .ui since you can use empty QWidget (without promoting it) as a container, then place a layout, and within the layout the QChartView.
The image shows an empty QWidget whose name is "chart_container" where the QChartView will be placed:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>210</x>
<y>280</y>
<width>88</width>
<height>33</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>340</x>
<y>80</y>
<width>88</width>
<height>33</height>
</rect>
</property>
<property name="text">
<string>Test Trade</string>
</property>
</widget>
<widget class="QWidget" name="chart_container" native="true">
<property name="geometry">
<rect>
<x>29</x>
<y>29</y>
<width>291</width>
<height>231</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
The code is then implemented by adding the QChartView to the container through a layout:
import sys
from PyQt5 import QtCore, QtWidgets, uic, QtChart
# load both ui file
uifile_1 = "UI/main.ui"
form_1, base_1 = uic.loadUiType(uifile_1)
class Example(base_1, form_1):
def __init__(self):
super(base_1, self).__init__()
self.setupUi(self)
data = (
(1, 7380, 7520, 7380, 7510, 7324),
(2, 7520, 7580, 7410, 7440, 7372),
(3, 7440, 7650, 7310, 7520, 7434),
(4, 7450, 7640, 7450, 7550, 7480),
(5, 7510, 7590, 7460, 7490, 7502),
(6, 7500, 7590, 7480, 7560, 7512),
(7, 7560, 7830, 7540, 7800, 7584),
)
series = QtChart.QCandlestickSeries()
series.setDecreasingColor(QtCore.Qt.red)
series.setIncreasingColor(QtCore.Qt.green)
ma5 = QtChart.QLineSeries() # 5-days average data line
tm = [] # stores str type data
# in a loop, series and ma5 append corresponding data
for num, o, h, l, c, m in data:
series.append(QtChart.QCandlestickSet(o, h, l, c))
ma5.append(QtCore.QPointF(num, m))
tm.append(str(num))
chart = QtChart.QChart()
chart.addSeries(series) # candle
chart.addSeries(ma5) # ma5 line
chart.setAnimationOptions(QtChart.QChart.SeriesAnimations)
chart.createDefaultAxes()
chart.legend().hide()
chart.axisX(series).setCategories(tm)
chart.axisX(ma5).setVisible(False)
chartview = QtChart.QChartView(chart)
self.chart_container.setContentsMargins(0, 0, 0, 0)
lay = QtWidgets.QHBoxLayout(self.chart_container)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(chartview)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
这篇关于将 QChartView 插入 ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 QChartView 插入 ui
基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01