是否可以使用qmlRegisterType函数将init参数传递给注册到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实例。我认为sites类也可以注册到qml中,并在qml中实例化时传递给routeconn,但我还没有找到这样做的方法。你知道吗
在Python中将类注册到QML:
qmlRegisterType(RoutesConn, 'RoutesConn', 1, 0, 'RoutesConn')
质量标准:
import RoutesConn 1.0
RoutesConn {
id: rconn
....
}
我希望在注册到qml之后的初始化过程中有一种方法可以将对象传递到类中,但事实似乎并非如此。你知道吗
TL;DR;不,不可能。你知道吗
QML希望通过qmlRegisterType注册的QObject只有一个构造函数,该构造函数将QObject作为父对象接收(qmlRegisterType不生成对象,只使其在QML中可用):
如果要传递其他对象,必须通过插槽或属性进行传递:
相关问题 更多 >
编程相关推荐