如何将初始化参数传递给注册了qmlRegisterType的类?

2024-09-30 16:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以使用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之后的初始化过程中有一种方法可以将对象传递到类中,但事实似乎并非如此。你知道吗


Tags: 对象实例方法instancefromimport属性qml
1条回答
网友
1楼 · 发布于 2024-09-30 16:27:06

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
    }
}

相关问题 更多 >