I am trying to use QML TableView to display QAbstractTableModel . The missing part of the equation seems to be the inability to have a variable number of columns in the TableView , despite the redefinition of QAbstractItemModel::roleNames , which Qt should indicate the number and name of my columns. I tried to verify this using only QML:
import QtQuick 2.0 import QtQuick.Controls 1.1 Rectangle { anchors.fill: parent property real showImage: 1.0 width: 500 TableView { id: myTable model: myModel // TableViewColumn { // role: "title"; title: "Name"; width: 200 // } } ListModel { id: myModel ListElement { title: "item one" } ListElement { title: "item two" } } }
When this is done, nothing is displayed, despite the TableView mode containing the ListElement with the roles defined in them.
However, if the above code is uncommented and defined by the TableViewColumn , then the column will display data for this role as expected, but the table still does not display other roles. Obviously, this will only work for a statically determined number of columns, and not for my case, when the number of columns is unknown until runtime.
The above example is basically the same as my real life example, except that my model is defined in C ++.
It seems that this could already be asked here , but he received no answer.
EDIT: I tried calling the javascript function:
function addColumnToTable(roleName) { var columnString = 'import QtQuick 2.3; import QtQuick.Controls 1.2; TableViewColumn {role: "' + roleName + '"; title: "' + roleName + '"; width: 40}'; var column = Qt.createQmlObject( columnString , myTable , "dynamicSnippet1") myTable.addColumn(column); }
From C ++:
QVariant roleName = "name"; QObject *root = view->rootObject(); QMetaObject::invokeMethod(root, "addColumnToTable", Q_ARG(QVariant, roleName));
This allowed me to dynamically add columns from C ++, although not from the model / view architecture. Yoanda's decision is much better than that.