Important: Please read the Qt Code of Conduct -

[ListView]How to use C++ to add Row for ListView?

  • I have no idea about it.. I tried to set ListView's model as a QStringList, but it doesn't update when I append string to QStringList...
    Thanks a lot...

  • There is an unofficial way.

    use QMetaObject::invokeMethod

    QQmlApplicationEngine engine(QUrl("qrc:/qml/main.qml"));
    QObject *topLevel = engine.rootObjects().value(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    if ( !window ) {
    qWarning("Error: Your root item has to be a Window.");
    return -1;
    QObject model = topLevel->findChild<QObject>("listmodel");
    if (model) {
    qDebug() << "model exists";
    QVariantMap newElement; // QVariantMap will implicitly translates into JS-object
    newElement.insert("name","from cpp");
    model, // for this object we will call method
    "append_", // actually, name of the method to call
    Q_ARG(QVariant, QVariant::fromValue(newElement)) // method parameter
    main.qml contains
    ListView {
    id: list_view1
    anchors.fill: parent
    delegate: Item {
    x: 5
    height: 40
    Row {
    id: row1
    spacing: 10
    Text {
    text: name
    font.bold: true
    anchors.verticalCenter: parent.verticalCenter
    model: ListModel {
    id: myModel
    objectName: "listmodel"
    function append_(newElement) {

Log in to reply