有多种方法可以从 python/C++ 修改 QML 元素的属性,每种方法都有其优点和缺点。
1. 从 QML 中提取引用
- 通过另一个对象通过findChildren获取QML对象。
- Modify or access the property with
setProperty()
or property()
, respectively or with QQmlProperty.
main.qml(qml 用于接下来的 2 个 .py)
import QtQuick 2.11
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
ApplicationWindow {
visible: true
width: Screen.width/2
height: Screen.height/2
Rectangle {
id: rectangle
x: 187
y: 92
width: 200
height: 200
color: "blue"
objectName: "foo_object"
}
}
1.1 setProperty()、property()。
import os
import sys
from PyQt5 import QtCore, QtGui, QtQml
from functools import partial
def testing(r):
import random
w = r.property("width")
h = r.property("height")
print("width: {}, height: {}".format(w, h))
r.setProperty("width", random.randint(100, 400))
r.setProperty("height", random.randint(100, 400))
def run():
myApp = QtGui.QGuiApplication(sys.argv)
myEngine = QtQml.QQmlApplicationEngine()
directory = os.path.dirname(os.path.abspath(__file__))
myEngine.load(QtCore.QUrl.fromLocalFile(os.path.join(directory, 'main.qml')))
if not myEngine.rootObjects():
return -1
r = myEngine.rootObjects()[0].findChild(QtCore.QObject, "foo_object")
timer = QtCore.QTimer(interval=500)
timer.timeout.connect(partial(testing, r))
timer.start()
return myApp.exec_()
if __name__ == "__main__":
sys.exit(run())
1.2 QQml 属性。
import os
import sys
from PyQt5 import QtCore, QtGui, QtQml
from functools import partial
def testing(r):
import random
w_prop = QtQml.QQmlProperty(r, "width")
h_prop = QtQml.QQmlProperty(r, "height")
print("width: {}, height: {}".format(w_prop.read(), w_prop.read()))
w_prop.write(random.randint(100, 400))
h_prop.write(random.randint(100, 400))
def run():
myApp = QtGui.QGuiApplication(sys.argv)
myEngine = QtQml.QQmlApplicationEngine()
directory = os.path.dirname(os.path.abspath(__file__))
myEngine.load(QtCore.QUrl.fromLocalFile(os.path.join(directory, 'main.qml')))
if not myEngine.rootObjects():
return -1
r = myEngine.rootObjects()[0].findChild(QtCore.QObject, "foo_object")
timer = QtCore.QTimer(interval=500)
timer.timeout.connect(partial(testing, r))
timer.start()
return myApp.exec_()
if __name__ == "__main__":
sys.exit(run())
这种方法的缺点是,如果对象与根对象的关系很复杂(有时很难使用 findChild 访问其他 QML 中的对象),则访问对象的部分会变得复杂,有时甚至是不可能的,因此此方法将失败。另一个问题是,当使用 objectName 作为主要搜索数据时,Python 层对 QML 层具有高度依赖性,因为如果在 QML 中修改 objectName,则必须修改 python 中的逻辑。另一个缺点是,如果不管理 QML 对象的生命周期,它可能会在 Python 不知情的情况下被消除,因此它会访问不正确的引用,导致应用程序意外终止。
2. 推送对 QML 的引用
- 创建一个具有相同类型属性的 QObject。
- 使用 setContextProperty 导出到 QML。
- 在 QObject 的属性和项目的属性之间进行绑定。
main.qml
import QtQuick 2.11
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
ApplicationWindow {
visible: true
width: Screen.width/2
height: Screen.height/2
Rectangle {
id: rectangle
x: 187
y: 92
width: r_manager.width
height: r_manager.height
color: "blue"
}
}
main.py
import os
import sys
from PyQt5 import QtCore, QtGui, QtQml
from functools import partial
class RectangleManager(QtCore.QObject):
widthChanged = QtCore.pyqtSignal(float)
heightChanged = QtCore.pyqtSignal(float)
def __init__(self, parent=None):
super(RectangleManager, self).__init__(parent)
self._width = 100
self._height = 100
@QtCore.pyqtProperty(float, notify=widthChanged)
def width(self):
return self._width
@width.setter
def width(self, w):
if self._width != w:
self._width = w
self.widthChanged.emit(w)
@QtCore.pyqtProperty(float, notify=heightChanged)
def height(self):
return self._height
@height.setter
def height(self, h):
if self._height != h:
self._height = h
self.heightChanged.emit(h)
def testing(r):
import random
print("width: {}, height: {}".format(r.width, r.height))
r.width = random.randint(100, 400)
r.height = random.randint(100, 400)
def run():
myApp = QtGui.QGuiApplication(sys.argv)
myEngine = QtQml.QQmlApplicationEngine()
manager = RectangleManager()
myEngine.rootContext().setContextProperty("r_manager", manager)
directory = os.path.dirname(os.path.abspath(__file__))
myEngine.load(QtCore.QUrl.fromLocalFile(os.path.join(directory, 'main.qml')))
if not myEngine.rootObjects():
return -1
timer = QtCore.QTimer(interval=500)
timer.timeout.connect(partial(testing, manager))
timer.start()
return myApp.exec_()
if __name__ == "__main__":
sys.exit(run())
缺点是需要多写一些代码。优点是该对象可以被所有 QML 访问,因为它使用 setContextProperty,另一个优点是如果 QML 对象被删除,它不会产生问题,因为仅消除了绑定。最后,通过不使用 objectName,依赖性不存在。
所以我更喜欢使用第二种方法,有关更多信息,请阅读从 C++ 与 QML 交互 https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c.