python - 从 Python 调用 QML 方法
问题描述
我不熟悉如何将 Python 与 QML 连接起来,所以如果我能得到一些指导,我会很高兴......
我想调用一个 QML 方法operaPieSeries()来操作一些 UI 字段。这是我的代码。为了实现它,我必须在main.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_())
main.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 操作一些 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)
}
}
推荐阅读
- java - 流处理 - 按名称在给定目录中搜索文件/目录
- webpack - 如何使用 Webpack 在 Backbone 应用程序中构建 HTML
- javascript - 对象数组的格式错误
- javascript - shopify product.liquid 文件中的 Javascript 变量
- python - 如何解决使用远程驱动程序在 Windows 上运行 IE 和 chrome 浏览器的“超时”问题
- javascript - 将搜索框文本复制到javascript select2中的下拉文本
- ibm-cloud - 在 IBM 云函数中将函数输出作为电子邮件发送
- cron - 如何抑制 cron 电子邮件反馈?
- nginx - Opencart 修改页面显示 403 Forbidden 错误
- ios - 检查 NSStream 上的输入是否可用时,主线程阻塞 NSStream