【问题标题】:How to Add Dynamic Data to a QML Table如何将动态数据添加到 QML 表
【发布时间】:2016-07-05 17:47:15
【问题描述】:

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

我不知道如何将模型添加到表中,除非该模型也在 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

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
    }
}

编辑

tableModel.append({'author': 'one', 'title': 'two'})

builtins.TypeError: unable to convert argument 0 of 

QAbstractListModel.append from 'dict' to 'QQmlV4Function*'

【问题讨论】:

  • 为什么不用ListModel类型的append方法呢? (doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#append-method)
  • “我不知道如何将模型添加到表中,除非该模型也在 QML 中。但我不知道如何为模型添加值。”对不起,什么?什么是模型类型,你想在哪里定义它,你想从哪一侧操作它?

标签: python pyqt qml qt5 pyqt5


【解决方案1】:

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

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

附:如果您想展示示例,请在评论中告诉我:]

编辑:添加代码

import QtQuick 2.3
import MyApplication 1.0

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

现在是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_()

【讨论】:

  • 举个例子会很有帮助:)
  • 示例已添加,您不能只是复制粘贴运行它,但它会给您提供思路,如何操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 1970-01-01
相关资源
最近更新 更多