Python 如何将动态数据添加到 QML 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36093417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:24:04  来源:igfitidea点击:

How to Add Dynamic Data to a QML Table

pythonpyqtqmlqt5pyqt5

提问by Siecje

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

我正在尝试从 Python 向表中添加行。我正在使用 QML 描述的 TableView。

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.

我不知道如何将模型添加到表中,除非模型也在 QML 中。但我不知道如何向模型添加值。

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

用户.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*'

采纳答案by Ford O.

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.

由于还没有人回答这个问题,我建议您使用一种解决方法:在 qml 中创建一个带有两个参数的 javascript 函数,然后直接从 QML 文件将元素添加到表中。

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

(显然你必须先从 python 调用函数,但那是小菜一碟......)

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

PS如果你想展示例子,请在评论中告诉我:]

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

现在python代码

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