具有动态列数的 QML TableView

2022-01-19 00:00:00 qt qtquick2 qml c++

我一直在尝试使用 QML TableView 来显示 QAbstractTableModel.等式的缺失部分似乎是在 TableView 中不可能有可变数量的列,尽管覆盖了应该告诉 Qt 的 QAbstractItemModel::roleNames我的专栏的编号和名称.我尝试仅使用 QML 对此进行测试:

I have been trying to use a QML TableView to display a QAbstractTableModel. The missing part of the equation seems to be that it is not possible to have a variable number of columns in the TableView, despite overriding QAbstractItemModel::roleNames which should tell Qt the number and name of my columns. I tried testing this out 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"
        }
    }
}

运行时,尽管 TableView 的模式包含 ListElements,其中定义了角色.

When run this doesn't show anything despite the TableView's mode containing ListElements with roles defined in them.

但是,如果取消注释上述代码并且定义了 TableViewColumn,则该列将按预期显示该角色的数据,但该表仍不会显示任何其他角色.显然,这仅适用于静态定义的列数,而不适用于直到运行时才知道列数的情况.

However if the above code is uncommented and a TableViewColumn is defined then the column will display data for that role as expected but the table will still not display any other roles. Obviously that will only work for a statically defined number of columns and not my case where the number of columns is not known until run time.

给出的示例与我的实际示例基本相同,只是我的模型是用 C++ 定义的.

The example given is basically the same as my real life example except that my model is defined in C++.

似乎这可能已经被问过这里但它没有得到任何回应.

It seems as if this may have already been asked here but it did not gain any response.

我曾尝试调用 javascript 函数:

I had tried calling a 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);
}

来自 C++:

QVariant roleName = "name";
QObject *root = view->rootObject();
QMetaObject::invokeMethod(root, "addColumnToTable", Q_ARG(QVariant, roleName));

这至少允许我从 C++ 动态添加列,尽管不是从模型/视图架构中.Yoann 的解决方案远比这要好.

This at least allowed me to dynamically add columns from C++ although not from within the model/view architecture. Yoann's solution is far and away better than this though.

推荐答案

您可以使用 resources 属性根据需要动态创建任意数量的 TableViewColumn>表格视图.

You could create dynamically as many TableViewColumn as you need, using the resources property of your TableView.

您必须在自定义模型类中添加一个方法,该方法将为您提供要显示的角色名称.

You will have to add a method in your custom model class which will give you the roleNames you want to display.

QML:

Component
{
    id: columnComponent
    TableViewColumn{width: 100 }
}

TableView {
    id: view
    anchors.fill: parent
    resources:
    {
        var roleList = myModel.customRoleNames
        var temp = []
        for(var i=0; i<roleList.length; i++)
        {
            var role  = roleList[i]
            temp.push(columnComponent.createObject(view, { "role": role, "title": role}))
        }
        return temp
    }

    model: myModel

MyModel.h:

class MyModel: public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(QStringList userRoleNames READ userRoleNames CONSTANT)

public:
    explicit MyModel(QObject *parent = 0);

    enum MyModelRoles {
        UserRole1 = Qt::UserRole + 1,
        UserRole2,
        ...
    };

    QStringList userRoleNames();
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    ...

private:
    QHash<int, QByteArray> roleNames() const;
    ...

};

MyModel.cpp:

...
...

QHash<int, QByteArray> MyModel::roleNames() const {
    QHash<int, QByteArray> roles = QAbstractListModel::roleNames ();
    roles[UserRole1] = "whatever";
    roles[UserRole2] = "youwant";
    return roles;
}

QStringList MyModel::userRoleNames() // Return ordered List of user-defined roles
{
    QMap<int, QString> res;
    QHashIterator<int, QByteArray> i(roleNames());
    while (i.hasNext()) {
        i.next();
        if(i.key() > Qt::UserRole)
            res[i.key()] = i.value();
    }
    return res.values();
}

...
...

相关文章