我试图使用python脚本在qt窗口(qml)中绘制数据(存储在csv文件中)。我正试着跟踪这个link。下面是我尝试过的代码:
main.qml
import QtQuick
import QtQuick.Window
import QtQuick.Controls
import './imports/Plot_Test'
import QtCharts 2.14
Window {
width: plot_rect.width
height: plot_rect.height
visible: true
title: "Plot_Test"
Rectangle {
id: plot_rect
width: 1296
height: 730
color: "#6e9ccc"
ChartView {
id: spline_view
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
anchors.topMargin: 20
anchors.rightMargin: 20
anchors.leftMargin: 20
SplineSeries{
id: plot
name: "xyz"
axisX: ValueAxis {
color: "#1c2b95"
gridLineColor: "#6e9ccc"
min: 0
max: 10
tickCount: 6
}
axisY: ValueAxis {
color: "#1c2b95"
gridLineColor: "#6e9ccc"
min: 0
max: 30000
tickCount: 6
}
XYPoint {
x: 1
y: 10000
}
XYPoint {
x: 2
y: 12000
}
}
Component.onCompleted: {
console.log("This is main.QML")
// var serie = spline_view.createSeries(ChartView.SeriesTypeSpline,"Random",axisX,axisY)
}
}
Connections {
target: plotter
function onGetSeries(series){
plot.XYSeries(series)
}
}
}
}main.py
# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys
from PySide6.QtCore import QCoreApplication, Qt, QUrl
from PySide6.QtWidgets import QApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCharts import QChartView
import data_plot
CURRENT_DIRECTORY = Path(__file__).resolve().parent
def main():
app = QApplication(sys.argv)
helper = data_plot.Helper()
# provider.pointsChanged.connect(helper.replace_points)
engine = QQmlApplicationEngine()
engine.rootContext().setContextProperty('plotter', helper)
filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
url = QUrl.fromLocalFile(filename)
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
QCoreApplication.exit(-1)
engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
engine.load(url)
sys.exit(app.exec())
if __name__ == "__main__":
main()data_plot.py
from email import policy
import pandas as pd
from PySide6.QtCore import QPointF, QObject, Signal, Slot
from PySide6.QtCharts import QChart, QChartView, QSplineSeries, QValueAxis, QXYSeries, QAbstractSeries
from PySide6 import QtCore
class Helper(QObject):
getSeries = Signal(list)
def __init__(self, parent=None):
QObject.__init__(self)
@Slot(QSplineSeries)
def update_series(self, series):
series.replace(self.generate_points())
@Slot(list)
def generate_points(self):
points = []
df = pd.read_csv('NK_Heavy.csv')
x = df['x']
for i in range(len(x)):
point = QPointF(i, x[i])
points.append(point)
self.getSeries.emit(points)
return points我试图访问qml元素(在本例中,splineseries: plot ),但是样条系列的XYPoint/XYSeries元素仍然没有更新。谁能指出我在这里的错误是什么,我如何解决它。谢谢
编辑 Ok,让我重新整理我的问题。如何在我的python代码中调用spline_view (它是chartview对象的id,或者plot是chartview文件中的splineseries对象的id ),这样我就可以在python中自己添加序列/数据。
发布于 2022-06-07 11:15:16
我在网上查了一下,发现了我的错误,我试图从python调用函数,但我没有把任何论据传递给它。以下是完整的解决方案:
data_plot.py
import pandas as pd
from PySide6.QtCore import QObject, QPointF, Slot
from PySide6.QtCharts import QChartView, QChart, QAbstractSeries, QSplineSeries, QValueAxis
from PySide6 import QtGui
class DataModel(QObject):
@Slot(QSplineSeries)
def fill_serie(self, plot):
self.df = pd.read_csv('gvk_normal_minus_ambient.csv')
x = self.df['x']
count = 0
for i in range(0,len(x)):
point = QPointF(count, x[i])
# point = x[i]
plot.append(point)
count += 1
plot.setProperty("color", QtGui.QBrush(QtGui.QColor("red")))
plot.setProperty("borderWidth", 4.0)
@Slot(QValueAxis)
def min_yaxis(self, yaxis):
yaxis.setProperty('min', min(self.df['x']))
@Slot(QValueAxis)
def max_yaxis(self, yaxis):
yaxis.setProperty('max', max(self.df['x']))
@Slot(QValueAxis)
def min_xaxis(self, xaxis):
xaxis.setProperty('min', 0)
xaxis.setProperty('tickInterval', 10)
@Slot(QValueAxis)
def max_xaxis(self, xaxis):
xaxis.setProperty('max', 30)main.qml
Component.onCompleted: {
console.log("This is main.QML")
var plot = spline_view.createSeries(ChartView.SeriesTypeSpline,"Random",axisX,axisY)
plotter.fill_serie(plot)
var ymax = plotter.max_yaxis(axisY)
var ymin = plotter.min_yaxis(axisY)
var xmax = plotter.max_xaxis(axisX)
var xmin = plotter.min_xaxis(axisX)
}以上部分,我改变了我的旧main.py,并找到了答案。
https://stackoverflow.com/questions/72517245
复制相似问题