【发布时间】:2020-08-05 01:43:24
【问题描述】:
我正在尝试获取一个 python 变量并在 QML 中使用它。我的实现是创建一个按钮,单击该按钮时,它将获取从我的程序生成的数据并将浮点值附加到线系列的 x 和 y 上。有什么建议吗?
这是我的程序中的一小段代码,可以复制我的问题:
# ======================================================================== #
from math import exp
class ErrorFunction:
def firstPoint(self, param):
if param < 0:
return -self.firstPoint(-param)
else:
p = 0.47047
a = [0.3480242, -0.0958798, 0.7478556]
t = 1.0 / (1.0 + p * param)
return 1 - t * (a[0] + t * (a[1] + t * a[2])) * exp(-(param ** 2))
def successivePoint(self, param):
return 1 - self.firstPoint(param)
# ======================================================================== #
# ======================================================================== #
import os
import sys
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtWidgets import QApplication
from ErrorFunction import ErrorFunction
class ErrorFunctionRunnable:
def __init__(self):
errorfunction = ErrorFunction()
n = 100
errorPoints = [(0, 0)] * n
for i in range(n):
x = i * 0.1 - 5.0
errorPoints[i] = (errorfunction.firstPoint(x), errorfunction.successivePoint(x))
print(errorPoints[i])
if __name__ == "__main__":
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(__file__), "gui.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
# ======================================================================== #
// ======================================================================== #
import QtQuick 2.0
import QtQuick.Controls 2.5
import QtQuick.Controls.Material 2.3
import QtCharts 2.3
ApplicationWindow {
id: applicationWindow
visible: true
width: 720
height: 780
title: qsTr("QML Test")
Material.background: Material.color(Material.Grey, Material.Shade900)
GroupBox {
id: groupBox
width: 114
anchors.top: parent.top
anchors.topMargin: 0
anchors.bottom: parent.bottom
anchors.bottomMargin: 0
anchors.left: parent.left
anchors.leftMargin: 0
Button {
id: button
x: 8
y: 0
width: 74
height: 48
text: qsTr("E_r")
font.pointSize: 10
Material.foreground: Material.color(Material.Grey, Material.Shade100)
Material.background: Material.color(Material.Grey, Material.Shade800)
onClicked: {
line.removeAllSeries()
/*
Run the "ErrorFunctionRunnable" init function to generate "errorPoints"
*/
}
}
}
GroupBox {
id: groupBox2
anchors.right: parent.right
anchors.rightMargin: 0
anchors.top: parent.top
anchors.topMargin: 0
anchors.left: parent.left
anchors.leftMargin: 114
anchors.bottom: parent.bottom
anchors.bottomMargin: 75
ChartView {
id: line
dropShadowEnabled: false
anchors.left: parent.left
anchors.leftMargin: 0
anchors.top: parent.top
anchors.topMargin: 0
anchors.right: parent.right
anchors.rightMargin: 0
anchors.bottom: parent.bottom
anchors.bottomMargin: 0
theme: ChartView.ChartThemeDark
antialiasing: true
backgroundColor: Material.color(Material.Grey, Material.Shade900)
LineSeries {
name: "Marshak Wave"
/*
Update the line series with the points from "errorPoints"
*/
}
}
}
GroupBox {
id: groupBox1
y: 704
height: 76
anchors.right: parent.right
anchors.rightMargin: 0
anchors.left: parent.left
anchors.leftMargin: 114
anchors.bottom: parent.bottom
anchors.bottomMargin: 0
}
}
// ======================================================================== #
【问题讨论】:
-
我不确定我还能展示什么。我已经删除了所有将 python 变量暴露给 QML 的尝试,因为它们显然不起作用。
-
第一个代码段是不言自明的。它所做的只是将浮点数添加到绘图点数组中。
-
这个项目有数千行代码来产生这些情节点。它不仅仅是一个随机数生成器。这是一个科学程序,它采用数学方程并运行加热管内某些温度和气体行为的测试序列,以生成显示马沙克波的图表
-
我发布的 QML 位只是我想要的一个示例,而是硬编码的。
-
好的我明白了,给我一点时间写点东西