Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PySide 6.4 and QML: Property-Binding doesn't work as expected
Forum Updated to NodeBB v4.3 + New Features

PySide 6.4 and QML: Property-Binding doesn't work as expected

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for python
1 Posts 1 Posters 646 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChristianL
    wrote on last edited by ChristianL
    #1

    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 property settings.name also is changed, but it doesn't happen. self.__name is always None . Did I miss something? I studied the Qt for Python documentation on the Property-Topic. Instead of the C++ Q_PROPERTY -Macro in Python the Property -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()
        }
    }
    

    Settings.py

    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()
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved