c++ - 具有动态列数的 QML TableView

标签 c++ qt qml qtquick2

我一直在尝试使用 QML TableView 来显示 QAbstractTableModel。等式的缺失部分似乎是在 TableView 中不可能有可变数量的列,尽管重写了 QAbstractItemModel::roleNames 应该告诉 Qt我的专栏的编号和名称。我尝试仅使用 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 的模式包含 ListElement,但运行时它不会显示任何内容s 中定义了角色。

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

除了我的模型是用 C++ 定义的之外,给出的示例与我的真实示例基本相同。

好像已经有人问过了here但没有得到任何回应。

编辑:我曾尝试调用一个 javascript 函数:

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++ 中动态添加列,尽管不是在模型/ View 架构中。不过,Yoann 的解决方案远远好于此。

最佳答案

使用 TableViewresources 属性,您可以根据需要动态创建任意数量的 TableViewColumn

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

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;
    ...

};

我的模型.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();
}

...
...

关于c++ - 具有动态列数的 QML TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27230818/

相关文章:

c++ - QML Camera 的无效/未定义媒体对象属性

c++ - 如何限制QInputDialog::getText的内容

c++ - 模仿基于 QApplication::palette() 的颜色样式行为

c++ - QtSerialPort 在错误的线程中实例化,导致信号/插槽失败

c++ - 程序无法在其他 Windows 机器上正常运行

qt - qml 无框窗口的阴影

c++ - QtQuick : Change expand icon in TreeView

c++ - 磁盘结构元素opencv vs Matlab

c++ - 读取文本文件时出错

c++ - 命名空间 'atomic' 中没有名为 'std' 的类型