作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以使用 qmlRegisterType 函数将初始化参数传递给注册到 QML 的 python 类?如果是这样,当在 QML 中实例化类时它们会被传递吗?
我已经使用 qmlRegisterType 注册了该类,但没有找到将另一个类实例作为对象传递的方法。我确实看到有一种方法可以注册扩展对象,但根据文档,这些只能是属性。我想传递另一个实例化的类,以便我可以在我注册到 QML 的类中访问它的方法和属性。
如何在 Python 中实例化该类:
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine, qmlRegisterType, QQmlComponent
from PySide2.QtCore import QObject
from app import Sites
from models import RoutesConn
def main():
# create the application instance
app = QApplication(sys.argv)
# create a QML engine
engine = QQmlApplicationEngine()
# instantiate Sites instance
sites = Sites()
# instantiate RoutesConn instance, and pass in sites instance
routesconn = RoutesConn(sites)
# this could be provided to qml as an object
engine.rootContext().setContextProperty('RoutesConn', routesconn)
但是,如果在qml中注册为类,则无法传递sites实例。我认为站点类也可以注册到 qml 并在 QML 中实例化时传递给 RoutesConn,但我还没有找到一种方法来做到这一点。
在 Python 中将类注册到 QML:
qmlRegisterType(RoutesConn, 'RoutesConn', 1, 0, 'RoutesConn')
QML:
import RoutesConn 1.0
RoutesConn {
id: rconn
....
}
我希望有一种方法可以在注册到 qml 后初始化期间将对象传递到类中,但情况似乎并非如此。
最佳答案
TL; DR;不,这是不可能的。
<小时/>QML 期望通过 qmlRegisterType 注册的 QObject 仅具有一个接收 QObject 作为父级的构造函数(qmlRegisterType 不会构建该对象,仅使其在 QML 中可用):
class Foo(QtCore.QObject):
def __init__(self, parent=None):
super().__init__(parent)
# ...
如果您想传递其他对象,则必须通过插槽或属性来完成:
from PySide2 import QtCore, QtGui, QtQml
class Bar(QtCore.QObject):
def test(self):
print("test")
class Foo(QtCore.QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.m_bar = None
@QtCore.Slot(Bar)
def load_bar(self, bar):
self.m_bar = bar
@QtCore.Slot()
def test_bar(self):
if self.m_bar is not None:
self.m_bar.test()
if __name__ == "__main__":
import os
import sys
app = QtGui.QGuiApplication(sys.argv)
QtQml.qmlRegisterType(Foo, 'TestComponents', 1, 0, 'Foo')
QtQml.qmlRegisterType(Bar, 'TestComponents', 1, 0, 'Bar')
engine = QtQml.QQmlApplicationEngine()
file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "main.qml")
engine.load(file)
if not engine.rootObjects():
sys.exit(-2)
sys.exit(app.exec_())
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls 2.5
import TestComponents 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
color: "whitesmoke"
Foo{
id: foo
Component.onCompleted: load_bar(bar)
}
Bar{
id: bar
}
Button {
text: "Cancel"
onClicked: foo.test_bar()
anchors.centerIn: parent
}
}
from PySide2 import QtCore, QtGui, QtQml
class Bar(QtCore.QObject):
def test(self):
print("test")
class Foo(QtCore.QObject):
barChanged = QtCore.Signal()
def __init__(self, parent=None):
super().__init__(parent)
self.m_bar = None
def getBar(self):
return self.m_bar
def setBar(self, bar):
if self.m_bar != bar:
self.m_bar = bar
self.barChanged.emit()
bar = QtCore.Property(Bar, fget=getBar, fset=setBar, notify=barChanged)
@QtCore.Slot()
def test_bar(self):
if self.m_bar is not None:
self.m_bar.test()
if __name__ == "__main__":
import os
import sys
app = QtGui.QGuiApplication(sys.argv)
QtQml.qmlRegisterType(Foo, "TestComponents", 1, 0, "Foo")
QtQml.qmlRegisterType(Bar, "TestComponents", 1, 0, "Bar")
engine = QtQml.QQmlApplicationEngine()
file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "main.qml")
engine.load(file)
if not engine.rootObjects():
sys.exit(-2)
sys.exit(app.exec_())
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls 2.5
import TestComponents 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
color: "whitesmoke"
Foo{
id: foo
bar: bar_object
}
Bar{
id: bar_object
}
Button {
text: "Cancel"
onClicked: foo.test_bar()
anchors.centerIn: parent
}
}
关于python - 如何将 __init__ 参数传递给使用 qmlRegisterType 注册的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57153525/
我是一名优秀的程序员,十分优秀!