2017-04-04 12 views
0

QMLとPyQtの初心者です。QMLページからPyQtを使って別のQMLページに移動するときにQObject(プロパティとsetproperty)を使用していました。 私は2番目のQMLページを表示することができました。そして、私はsetpropertyを使用するときにこのエラーを受け取ります:AttributeError: 'NoneType' has no attribute setPropertyPyQtを使って1つのQMLページから別のQMLページに移動するには

私の第2のページQML "Classe.qml"は、テキストフィールドが含まれている、私は私が私の最初のページ"main.qml"のボタンをクリックすると、テキストフィールドの変更を表示された2番目のページがあることを何をしたいのか、私たちは"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()) 
+1

qmlファイルをページとして扱おうとしないでください。彼らはクラスです。一つのメインのqmlファイルに 'StackView'や' states'や 'Loaders'のようなもので他のファイルからの使用クラスを作ります。また、pythonから 'findChild'と' setProperty'を呼び出さないでください。 qmlはクラスのライブラリとしてPythonコードを使用する必要があります.qmlはPythonで実装したクラスのオブジェクトを作成できます。 – Velkan

+0

あなたの助けを借りてありがとうございます: 実際には、リストrootObjectの2番目の行を追加すれば十分です。デフテスト3(自己): engine.load( 'Classe.qml') win1 = engine.rootObjects()[1] win1.findChild(QObject、 "labelCo")。setProperty( "text"、 "HELLO WORLD ") – karahman

答えて

-1

それは良いことだ、それはあなたの助けに感謝を解決しています: 実際には、リストrootObjectで2行目を追加するのに十分です。

def test3(self): 

    engine.load('Classe.qml') 
    win1 = engine.rootObjects()[1] 
    win1.findChild(QObject, "labelCo").setProperty("text", "HELLOWORLD") 
関連する問題