0
事实上,我是QML和PyQt的初学者,我发现使用PyQt从一个QML页面过去的问题,然后我们使用QObject(属性和setproperty)。 所以我能够显示第二个QML页面,然后当我使用setproperty时,我会得到这个错误:AttributeError: 'NoneType' has no attribute setProperty
。从一个QML页面移动到另一个使用PyQt
我的第二页QML "Classe.qml"
包含文本字段,我想这样做,当我点击我的第一"main.qml"
页面的按钮,就会有出现,然后在文本字段更改第二页,我们写"hello world"
。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Classe
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSlot, QVariant
# Classe servant dans l'interaction.
class MainApp(QObject):
def __init__(self, context, parent=None):
super(MainApp, self).__init__()
self.win = parent
self.win.findChild(QObject, "ObjClasse").clicked.connect(self.test3)
self.ctx = context
def test3(self):
engine.load('Classe.qml')
self.win.findChild(QObject, "labelCo").setProperty("text", "hello world") ##l'erreur est ici
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
ctx = engine.rootContext()
engine.load('main.qml')
win = engine.rootObjects()[0]
py_mainapp = MainApp(ctx,win)
ctx.setContextProperty("py_MainApp", py_mainapp)
win.show()
sys.exit(app.exec())
不要试图将qml文件视为页面。他们是班级。使一个主要的qml文件成为一个类似于'StackView',状态或'Loaders'的其他文件的类。另外,不要试图从python调用findChild和setProperty。 python代码应该被qml用作一个类库:qml可以创建你在python中实现的类的对象。 – Velkan
这很好,它的求解感谢您的帮助: 实际上,在列表rootObject中添加第二行就足够了。 def test3(self): engine.load('Classe.qml') win1 = engine.rootObjects()[1] win1.findChild(QObject,“labelCo”)。setProperty(“text”,“HELLO WORLD “) – karahman