【发布时间】:2018-03-21 22:40:59
【问题描述】:
我对 Qt/PyQt 还很陌生,目前正在为一些基本功能而苦苦挣扎。我想要做的是,从 python 动态加载 QML-Views (*.qml) 文件并动态替换特定内容。例如,一个复选框被选中,我当前视图的一部分被另一个 qml 文件替换。首先我想通过 PyQt 提供这个逻辑,但 StackView 似乎是一个更好的主意 (multiple qml files in pyqt)。
但是,在这种情况下,我无法将属性注入到我的 QML 文件中。我只能将一个属性注入到 rootContext 中。然而,这限制了我的 QML 视图的使用,因为我一次只能使用一个视图(相同类型)。我想将属性动态注入到 QML-Views 中,并使它们只对这个特定的视图可见。在这种情况下,我可以在后端使用多个对象多次使用同一个视图来捕获信号。
这是我的 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
}
这里是我尝试加载的文件(TestViewButton.qml):
import QtQuick 2.9
import QtQuick.Controls 1.4
Button {
id: test
text: "test"
objectName : "Button"
onClicked: {
configurator.sum(1, 2)
configurator.info("TestOutput")
}
}
Python 文件加载 QML-View(组件)
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对象(SignalHandler.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 进行任何查看)。不工作的是 component.setProperty.... 部分。如果我在引擎的 rootContext 中设置属性,它可以正常工作(调用 SignalHandler 方法)。已经检查过类似的主题(如Load a qml component from another qml file dynamically and access that component's qml type properties ...)
这可能吗,还是我这里有什么问题?
谢谢
【问题讨论】:
-
什么是 SignalHandler?
-
我也添加了 SignlaHandler 代码。它只是一个简单的 QObject,用于处理来自视图的一些调用。
标签: python pyqt qml pyqt5 qqmlcomponent