【问题标题】:Invoking QML method from Python从 Python 调用 QML 方法
【发布时间】:2020-06-12 08:47:56
【问题描述】:

我不熟悉如何将 Python 与 QML 连接,所以如果我能得到一些指导,我会很高兴...

我想调用一个 QML 方法 ma​​nipulatePieSeries() 来操作一些 UI 字段。这是我的代码。为了实现它,我必须在 ma​​in.py 中添加什么?

ma​​in.py

import os
import sys

from PyQt5 import QtCore, QtGui, QtWidgets, QtQuick, QtChart

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    view = QtQuick.QQuickView()

    filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "main.qml")

    view.setSource(QtCore.QUrl.fromLocalFile(filename))
    view.show()

    sys.exit(app.exec_())

ma​​in.qml

import QtQuick 2.7
import QtQuick.Window 2.0
import QtQuick.Controls 1.4
import QtCharts 2.1

Grid {
    id: grid1
    width: 1024
    height: 600
    spacing: 10
    rows: 1
    columns: 2

    VPieModelMapper {
        id: mapper0
        series: serie0
        labelsColumn: 0
        valuesColumn: 1
        firstRow: 0
        rowCount: 100
    }

    ChartView {
        id: chart
        width: 600
        height: 600
        antialiasing: true
        animationDuration: 1000
        animationOptions: ChartView.AllAnimations
        title: "MyTitle"
        legend.visible: false

        PieSeries {
            id: serie0
            name: "Outer Ring"
            size: 0.75
            holeSize: 0.7
            function manipulatePieSeries(param1, param2, param3) {
                console.log("Params: ", param1, param2, param3)
            }
        }
    }
}

【问题讨论】:

    标签: python pyqt qml pyqt5


    【解决方案1】:

    如果您想从 python 操作一些 QML 信息,逻辑是创建一个 QObject,其中使用您要发送到 QML 的参数发出信号并使用 Connections 在 QML 中建立连接:

    import os
    import random
    import sys
    
    from PyQt5 import QtCore, QtGui, QtWidgets, QtQuick
    
    
    class Helper(QtCore.QObject):
        customSignal = QtCore.pyqtSignal(float, float, float, arguments=['param1', 'param2', 'param3'])
    
        def call_manipulatePieSeries(self, param1, param2, param3):
            self.customSignal.emit(param1, param2, param3)
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
    
        helper = Helper()
    
        view = QtQuick.QQuickView()
        view.rootContext().setContextProperty("helper", helper)
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "main.qml")
        view.setSource(QtCore.QUrl.fromLocalFile(filename))
        view.show()
    
        def on_timeout():
            helper.call_manipulatePieSeries(
                random.uniform(10, 100), random.uniform(10, 100), random.uniform(10, 100)
            )
    
        timer = QtCore.QTimer(interval=1000, timeout=on_timeout)
        timer.start()
    
        sys.exit(app.exec_())
    import QtQuick 2.7
    import QtQuick.Window 2.0
    import QtQuick.Controls 1.4
    import QtCharts 2.1
    
    Grid {
        id: grid1
        width: 1024
        height: 600
        spacing: 10
        rows: 1
        columns: 2
    
        VPieModelMapper {
            id: mapper0
            series: serie0
            labelsColumn: 0
            valuesColumn: 1
            firstRow: 0
            rowCount: 100
        }
    
        ChartView {
            id: chart
            width: 600
            height: 600
            antialiasing: true
            animationDuration: 1000
            animationOptions: ChartView.AllAnimations
            title: "MyTitle"
            legend.visible: false
    
            PieSeries {
                id: serie0
                name: "Outer Ring"
                size: 0.75
                holeSize: 0.7
                function manipulatePieSeries(param1, param2, param3) {
                    console.log("Params: ", param1, param2, param3)
                }
            }
        }
        Connections{
            target: helper
            onCustomSignal: serie0.manipulatePieSeries(param1, param2, param3)
        }
    }

    【讨论】:

      猜你喜欢
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多