How to Add Dynamic Data to a QML Table

33,503

Since nobody answered the question yet I will suggest you to use a workaround: Create a javascript function in qml with two arguments and add elements into to table right from QML file.

(Obviously you have to call the function from python first, but thats a piece of cake...)

P.S. If you wanna show example let me know in comment :]

EDIT: code added

import QtQuick 2.3
import MyApplication 1.0

QPythonBinding{
id: binding
signal addElement(string param1, string param2)
    onAddElement: {
        myModel.append({"key1" : param1, "key2" : param2})
    }
}

now python code

class QPythonBinding(QQuickItem):
    def __init__(self, parent=None):
        super(QPythonBinding, self).__init__(parent)

    addElement = pyqtSignal(str, str)   #you call it like this  - addElement.emit("name", "value")


if __name__ == '__main__':
    import sys
    app = QGuiApplication(sys.argv)

    qmlRegisterType(QPythonBinding, "MyApplication", 1, 0, "QPythonBinding")
    view = QQuickView()

    view.show()
    app.exec_()
Share:
33,503
Siecje
Author by

Siecje

Updated on July 09, 2022

Comments

  • Siecje
    Siecje almost 2 years

    I'm trying to add rows to a table from Python. I'm using a TableView described with QML.

    I can't figure out how to add a model to the table, unless the model is also in QML. But I can't figure out how to add values to the model.

    import sys
    from PyQt5.QtCore import QAbstractTableModel, QObject, QUrl
    from PyQt5.QtQml import QQmlApplicationEngine
    from PyQt5.QtQuick import QQuickView
    from PyQt5.QtWidgets import QApplication
    
    
    
    myApp = QApplication(sys.argv)
    
    engine = QQmlApplicationEngine()
    context = engine.rootContext()
    context.setContextProperty("main", engine)
    
    engine.load('users.qml')
    
    mainWin = engine.rootObjects()[0]
    
    # Add items
    userTable = mainWin.findChild(QObject, "userTable")
    tableModel = mainWin.findChild(QObject, "libraryModel")
    tableModel.setData(tableModel.index(0), "one")
    tableModel.setData(tableModel.index(1), "one")
    
    mainWin.show()
    
    sys.exit(myApp.exec_())
    

    users.qml

    import QtQuick 2.3
    import QtQuick.Controls 1.2
    
    ApplicationWindow {
        ListModel {
            id: libraryModel
            objectName: "libraryModel"
            ListElement {
                title: "A Masterpiece"
                author: "Gabriel"
            }
            ListElement {
                title: "Brilliance"
                author: "Jens"
            }
            ListElement {
                title: "Outstanding"
                author: "Frederik"
            }
        }
    
        TableView {
            objectName: "userTable"
            anchors.fill: parent
            TableViewColumn {
                role: "title"
                title: "Title"
            }
            TableViewColumn {
                role: "author"
                title: "Author"
            }
            model: libraryModel
        }
    }
    

    Edit

    tableModel.append({'author': 'one', 'title': 'two'})
    
    builtins.TypeError: unable to convert argument 0 of 
    
    QAbstractListModel.append from 'dict' to 'QQmlV4Function*'
    
  • Siecje
    Siecje about 8 years
    An example would be helpful :)
  • Ford O.
    Ford O. about 8 years
    Example was added, you cant just copy paste run it, but it will give you the idea, how to do it.