Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Use QVariantMap as model for TableView



  • Is it possible to use QVariantMap from C++ as a model for a TableView in QML?
    Basically what I want is the keys from a QVariantMap on one column, and their associated values on another column.
    Thanks!



  • @kimcheetos There is no easy way. Only through QAbstractItemModel(or other model class) subclassing.


  • Qt Champions 2017

    You can try doing something like follows.

    QList<QObject*> dataList;
    dataList.append(new DataObject("Item 1", "red"));
    dataList.append(new DataObject("Item 2", "green"));
    dataList.append(new DataObject("Item 3", "blue"));
    dataList.append(new DataObject("Item 4", "yellow"));
    
    QQmlApplicationEngine engine;
    
    QQmlContext *ctxt = engine.rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
    
    colour and name are Q_PROPERTY defined in DataObject.
    
    TableView {
        TableViewColumn {
            role: "color"
            title: "Title"
            width: 100
        }
        TableViewColumn {
            role: "name"
            title: "Author"
            width: 200
        }
        model: myModel
    }

Log in to reply