如何将__init__参数传递给使用qmlRegisterType注册的类?

时间:2019-07-22 20:49:05

标签: python qml pyside2

是否可以使用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实例。我认为,当在QML中实例化网站类时,也可以将其注册到qml并传递给RoutesConn,但是我还没有找到一种方法。

在Python中将类注册到QML:

qmlRegisterType(RoutesConn, 'RoutesConn', 1, 0, 'RoutesConn')

QML:

import RoutesConn 1.0

RoutesConn {
    id: rconn

    ....
}

我希望有一种方法可以在向qml注册后的初始化过程中将对象传递到类中,但事实并非如此。

1 个答案:

答案 0 :(得分:0)

TL; DR; 不,不可能。


QML希望通过qmlRegisterType注册的QObject仅具有一个将QObject作为父对象接收的构造函数(qmlRegisterType不会构建该对象,仅使其在QML中可用):

class Foo(QtCore.QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        # ...

如果要传递其他对象,则必须通过Slot或Property:

  • 调用Component中的插槽。已完成:
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
    }
}