Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向QML公开一个循环变化的python变量_Python_Qml_Pyside2 - Fatal编程技术网

向QML公开一个循环变化的python变量

向QML公开一个循环变化的python变量,python,qml,pyside2,Python,Qml,Pyside2,我试图获取一个python变量并在QML中使用它。我的实现是创建一个按钮,单击该按钮时,将获取程序生成的数据,并将浮点值附加到直线序列的x和y上。有什么建议吗 下面是我的程序中的一段代码,它将复制我的问题: 。=================================================================================================================================================

我试图获取一个python变量并在QML中使用它。我的实现是创建一个按钮,单击该按钮时,将获取程序生成的数据,并将浮点值附加到直线序列的x和y上。有什么建议吗

下面是我的程序中的一段代码,它将复制我的问题:

。===================================================================================================================================================================================================================================================================================================================================================================================================#
从数学导入exp
类错误函数:
def第一点(自身,参数):
如果参数<0:
return-self.firstPoint(-param)
其他:
p=0.47047
a=[0.3480242,-0.0958798,0.7478556]
t=1.0/(1.0+p*param)
返回1-t*(a[0]+t*(a[1]+t*a[2])*exp(-(参数**2))
def成功点(自身,参数):
返回1-自身第一点(参数)
# ========================================================================  #
。===================================================================================================================================================================================================================================================================================================================================================================================================#
导入操作系统
导入系统
从PySide2.QtQml导入QQmlApplicationEngine
从PySide2.QtWidgets导入QApplication
从ErrorFunction导入ErrorFunction
类ErrorFunctionRunnable:
定义初始化(自):
errorfunction=errorfunction()
n=100
错误点=[(0,0)]*n
对于范围(n)中的i:
x=i*0.1-5.0
errorPoints[i]=(errorfunction.firstPoint(x),errorfunction.successivePoint(x))
打印(错误点[i])
如果名称=“\uuuuu main\uuuuuuuu”:
os.environ[“QT\U快速控制\U样式”]=“材质”
app=QApplication(sys.argv)
引擎=QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(_文件__),“gui.qml”))
如果不是engine.rootObjects():
系统出口(-1)
sys.exit(app.exec_())
# ========================================================================  #
/==========================================================================================================================================================================================================================================================================================================================================================================================#
导入QtQuick 2.0
导入QtQuick.Controls 2.5
导入QtQuick.Controls.Material 2.3
导入QT2.3图表
应用程序窗口{
id:applicationWindow
可见:正确
宽度:720
身高:780
标题:qsTr(“QML测试”)
材质.背景:材质.颜色(材质.灰色,材质.阴影900)
分组框{
id:groupBox
宽度:114
anchors.top:parent.top
0.topMargin:0
.bottom:parent.bottom
下边距:0
anchors.left:parent.left
0.leftMargin:0
钮扣{
id:按钮
x:8
y:0
宽度:74
身高:48
正文:qsTr(“E_r”)
字体大小:10
材质.前景:材质.颜色(材质.灰色,材质.阴影100)
材质.背景:材质.颜色(材质.灰色,材质.阴影800)
再次点击:{
line.removeAllSeries()
/*
运行“ErrorFunctionRunnable”初始化函数以生成“errorPoints”
*/
}
}
}
分组框{
id:groupBox2
anchors.right:父项.right
0.rightMargin:0
anchors.top:parent.top
0.topMargin:0
anchors.left:parent.left
0.leftMargin:114
.bottom:parent.bottom
下边距:75
海图{
id:行
dropShadowEnabled:false
anchors.left:parent.left
0.leftMargin:0
anchors.top:parent.top
0.topMargin:0
anchors.right:父项.right
0.rightMargin:0
.bottom:parent.bottom
下边距:0
主题:ChartView.ChartThemeDark
抗锯齿:真
背景色:材质。颜色(材质。灰色,材质。阴影900)
系列{
名称:“马沙克波”
/*
使用“errorPoints”中的点更新线系列
*/
}
}
}
分组框{
id:groupBox1
y:704
身高:76
anchors.right:父项.right
0.rightMargin:0
anchors.left:parent.left
0.leftMargin:114
.bottom:parent.bottom
下边距:0
}
}
// ========================================================================  #

一种可能的解决方案是创建一个QObject,该QObject通过一种方法向QML公开QPointF列表,此外,必须更新轴的限制,如下所示:

导入数学
导入操作系统
导入系统
从PySide2.QtCore导入QObject、QPointF、Slot
从PySide2.QtQml导入QQmlApplicationEngine
从PySide2.QtWidgets导入QApplication
类错误函数:
def第一点(自身,参数):
如果参数<0:
return-self.firstPoint(-param)
其他:
p=0.47047
a=[0.3480242,-0.0958798,0.7478556]
t=1.0/(1.0+p*param)
返回1-t*(a[0]+t*(a[1]+t*a[2])*math.exp(-(参数**2))
def成功点(自身,参数):
返回1-自身第一点(参数)
类桥(QObject):
@插槽(结果=“QVariantList”)
def生产(自):
f=ErrorFunction()
值=[]
n=100
对于范围(n)中的i:
x=i*0.1-5.0
p=QPointF(f.第一个点(x),f.第二个点(x))
附加值(p)
返回值
如果名称=“\uuuuu main\uuuuuuuu”:
os.environ[“QT\U快速控制\U样式”]=“材质”
app=QApplication(sys.argv)
桥=桥()
引擎=QQmlApplicationEngine()
engine.rootContext().setContextProperty(“桥”,桥)
engine.load(os.path.join(os.path.dirname(_文件__),“gui.qml”))
如果不是engine.rootObjects():
系统出口(-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.removePoints(0, line.count)
                var x_min = line.axisX.min
                var x_max = line.axisX.max
                var y_min = line.axisY.min
                var y_max = line.axisY.max

                var values = bridge.produce()
                for(var i in values){
                    line.append(values[i].x, values[i].y)
                    x_min = Math.min(x_min, values[i].x)
                    x_max = Math.max(x_max, values[i].x)
                    y_min = Math.min(y_min, values[i].y)
                    y_max = Math.max(y_max, values[i].y)
                }
                line.axisX.min = x_min
                line.axisX.max = x_max
                line.axisY.min = y_min
                line.axisY.max = y_max
            }
        }
    }

    GroupBox {
        id: groupBox2
        anchors.fill: parent
        anchors.rightMargin: 0
        anchors.topMargin: 0
        anchors.leftMargin: 114
        anchors.bottomMargin: 75

        ChartView {
            id: view
            dropShadowEnabled: false
            anchors.fill: parent
            anchors.margins: 0
            theme: ChartView.ChartThemeDark
            antialiasing: true

            backgroundColor: Material.color(Material.Grey, Material.Shade900)
            LineSeries {
                id: line
                name: "Marshak Wave"
            }
        }
    }
    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
    }
}
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: {
                bridge.fill(line)
            }
        }
    }

    GroupBox {
        id: groupBox2
        anchors.fill: parent
        anchors.rightMargin: 0
        anchors.topMargin: 0
        anchors.leftMargin: 114
        anchors.bottomMargin: 75

        ChartView {
            id: view
            dropShadowEnabled: false
            anchors.fill: parent
            anchors.margins: 0
            theme: ChartView.ChartThemeDark
            antialiasing: true

            backgroundColor: Material.color(Material.Grey, Material.Shade900)
            LineSeries {
                id: line
                name: "Marshak Wave"
            }
        }
    }
    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
    }
}