PySide 6.4 and QML: Property-Binding doesn't work as expected
Unsolved
Qt for Python
-
I wanted to try out QML and Property-Binding with python. In the past I used Widgets only, but I did some QML programming under C++, and it worked as expected.
I would expect that every time the text in
TextField
is changed the propertysettings.name
also is changed, but it doesn't happen.self.__name
is alwaysNone
. Did I miss something? I studied the Qt for Python documentation on the Property-Topic. Instead of the C++Q_PROPERTY
-Macro in Python theProperty
-Function is used with getter, setter and notifier. I decorated my class with@QmlElement
. Here is the complete Code of my sample application:AppWindow.qml
import QtQuick.Controls import QtQuick.Layouts import QtQuick import com.example.app 1.0 Window { id: settingsDialog visible: true width: 500 height: 250 Settings { id: settings } GridLayout { columns: 2 anchors.fill: parent Text { text: qsTr("Your Name") } TextField { id: name text: settings.name } Button { text: "Load" onClicked: { console.log("Load") settings.load() } } Button { text: "Save" onClicked: { console.log("Save") settings.save() } } } Component.onCompleted: { console.log("Component.onCompleted") settings.load() } }
QML_IMPORT_NAME = "com.example.app" QML_IMPORT_MAJOR_VERSION = 1 from PySide6.QtCore import QObject, Signal, Slot, Property, QSettings from PySide6.QtQml import QmlElement KEY_PERSONAL_NAME = 'personal/name' @QmlElement class Settings(QObject): def __init__(self, parent=None): QObject.__init__(self, parent) self.__name = None @Slot() def load(self): self.__settings = QSettings('settings.ini', QSettings.IniFormat) self.__name = self.__settings.value(KEY_PERSONAL_NAME) @Slot() def save(self): self.__settings.setValue(KEY_PERSONAL_NAME, self.__name) def get_name(self): return self.__name @Slot(str) def set_name(self, name): if self.__name != name: self.__name = name self.name_changed.emit(name) name_changed = Signal(str) name = Property(str, fget=get_name, fset=set_name, notify=name_changed) if __name__ == '__main__': import sys from PySide6.QtCore import QUrl from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine, qmlRegisterType from Settings import Settings app = QGuiApplication(sys.argv) qmlRegisterType(Settings, "com.example.app", 1,0, "Settings") engine = QQmlApplicationEngine(QUrl("AppWindow.qml")) app.exec()