How to Add Dynamic Data to a QML Table

三世轮回 提交于 2021-02-09 11:46:22

问题


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*'

回答1:


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_()


来源:https://stackoverflow.com/questions/36093417/how-to-add-dynamic-data-to-a-qml-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!