Max92 | 2021-05-30 22:40:05 UTC | #1
I am having two different QML files. In the main.qml I am using a Loader to call another QML file (other.qml). The second file contains a TableView which I want to access with Python (main.py) by creating a class implementing QtCore.QAbstractTableModel. Unfortunately, I am not able to access activityTable with Python when loading the main.qml in main.py. Already, the assignment of the model in main.qml fails. How do I make this TableView accessable in Python?
main.qml
TableView {
    id: activityTable
    anchors.fill: parent
    columnSpacing: 1
    rowSpacing: 1
    clip: true
    model: ActivityTableModel {}
    delegate: Rectangle {
        implicitWidth: 100
        implicitHeight: 50
        Text {
            text: display
        }
    }
}
other.qml
Loader {
    id: pagesOther
    anchors.fill: parent
    source: Qt.resolvedUrl("pages/other.qml")
}
main.py
if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    main = MainWindow()
    engine.rootContext().setContextProperty("backend", main)
    engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))
I am using QtCreator, QtQuick 2.15, PySide2 and Python 3.9.
Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PySide6 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!