0

我有一个QAbstractListModelC++ 模型类和一个 QtQuick2 ListView

出于测试目的,我还创建了一个 QtWidgetsQListView并附加到相同的模型以进行比较。

我想告诉视图应该使用覆盖的项目禁用哪些项目QAbstractListModel::flags(),如下例所示:

Qt::ItemFlags flags(const QModelIndex& index) const
{
    Qt::ItemFlags f = QAbstractListModel::flags(index);
    if(index.isValid())
    {
        f |= Qt::ItemIsUserCheckable;

        if(index.row() % 3 == 2)
            f &= ~Qt::ItemIsEnabled;
    }
    return f;
}

在这里完成模型类

遗憾的是 QtQuick2 ListVie 完全忽略了这个标志,而 QListView 没有:

左:QtWidgets QListView,右:QtQuick2 ListView

我希望 item2 和 item5 在 QtQuick2 中也被禁用。如何做到这一点?

4

1 回答 1

0

如果您使用 CheckDelegate,您应该通过委托属性对其进行调整:

   delegate: CheckDelegate {
        text: model.display
        checked: model.checkState
        enabled: <here is your predicate, for example "index % 2" for each odd item>
    }

这是因为 QML 使用与 QWidgets 相反的不同方法来可视化数据。

于 2017-07-20T12:02:03.630 回答