向QMLComponen注入属性

2024-05-19 09:32:50 发布

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

我对Qt/PyQt相当陌生,目前正在努力实现一些基本功能。我要做的是,从python动态加载QML视图(*.QML)文件并动态替换特定的内容。例如,复选框被选中,我当前视图的一部分被另一个qml文件替换。首先,我想通过PyQt提供这个逻辑,但是StackView似乎是一个更好的主意(multiple qml files in pyqt)。在

但是,在这种情况下,我无法将属性注入到QML文件中。我只能将属性注入rootContext。但是这限制了QML视图的使用,因为我一次只能使用一个视图(相同类型)。我想将属性动态地注入到QML视图中,并使它们只对这个特定的视图可见。在这种情况下,我可以多次使用同一视图,在后端使用多个对象来捕捉信号。在

这是我的SimpleMainWindow.qml文件(主视图: 在

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
import QtQuick.Controls 1.4

ApplicationWindow {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    objectName : "WINDOW"
    property ApplicationWindow appWindow : window

}

这里是我要加载的文件(测试视图按钮.qml)公司名称:

^{pr2}$

Python文件加载QML视图(组件)

from PyQt5.QtCore import QObject, QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine, QQmlComponent

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    engine.load("qml/SimpleMainWindow.qml")
    engine.quit.connect(app.quit)

    rootWindow = engine.rootObjects()[0].children()[0].parent()

    # create component
    component = QQmlComponent(engine)
    component.loadUrl(QUrl("qml/TestViewButton.qml"))

    configurator = SignalHandler()
    component.setProperty("configurator", configurator)

    itm = component.create()
    itm.setParentItem(rootWindow.children()[0])

    itm.setProperty("configurator", configurator)

    app.exec_()

以及我用来处理来自视图的信号的python对象(信号处理程序.py)公司名称:

from PyQt5.QtCore import QObject, pyqtSlot

class SignalHandler(QObject):

    @pyqtSlot(int, int)
    def sum(self, arg1, arg2):
        print("Adding two numbers")

    @pyqtSlot(str)
    def info(self, arg1):
        print("STR " + arg1)

按钮加载良好(顺便问一下,有没有更好的方法来识别我想添加按钮的父级,而没有查看findChild)。不起作用的是组件.setProperty.... 部分。如果我在引擎的rootContext中设置该属性,那么它工作得很好(将调用SignalHandler方法)。已经检查过类似的主题(如Load a qml component from another qml file dynamically and access that component's qml type properties…)

这有可能吗,还是我有什么不对劲?在

谢谢


Tags: 文件fromimport视图属性动态qml按钮
1条回答
网友
1楼 · 发布于 2024-05-19 09:32:50

据我所知,您希望只在TestViewButton.qml中加载配置对象,而它在SimpleMainWindow.qml中不可见。在

要做到这一点,TestViewButton.qml在加载时必须有自己的QQmlContext,而不是{}。在

为了测试我的响应并观察其行为,我们将创建一个类似的按钮来尝试使用configurator,如果按下此按钮,它将抛出一个错误,指出该属性不存在,但是如果加载的按钮被{}按下,则该按钮应该正常工作。在

质量管理/SimpleMainWindow.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
import QtQuick.Controls 1.4

ApplicationWindow {
    id: window
    visible: true
    width: 640
    color: "red"
    height: 480
    title: qsTr("Hello World")

    Button {
        x: 100
        y: 100
        text: "ApplicationWindow"
        onClicked: {
            console.log("ApplicationWindow")
            configurator.sum(1, 2)
            configurator.info("TestOutput")
        }
    }
}

正如我之前所说,我添加了一个新的上下文组件:

^{pr2}$

最后我们得到以下输出:

qml: Component
Adding two numbers
STR TestOutput
qml: ApplicationWindow
file:///xxx/qml/SimpleMainWindow.qml:20: ReferenceError: configurator is not defined
qml: Component
Adding two numbers
STR TestOutput
qml: ApplicationWindow
file:///xxx/qml/SimpleMainWindow.qml:20: ReferenceError: configurator is not defined

在那里我们观察期望的行为。{a1}可以在下面的示例中找到。在

相关问题 更多 >

    热门问题