首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能让QColumnView用我的QAbstractItemModel派生类显示多行数据

不能让QColumnView用我的QAbstractItemModel派生类显示多行数据
EN

Stack Overflow用户
提问于 2016-05-16 03:37:57
回答 1查看 331关注 0票数 1

我正在使用QAbstractItemModel实现一个自定义模型,我希望在QColumnView中显示该模型。模型中的数据是目录和文件的简单层次结构,每个目录都可以包含文件和其他目录。

我的QAbstractItemModel派生类称为MyModel。它依赖于另外两个QObject派生类: Directory和Item。目录有几种方法,用于返回它包含的项目数(包括子目录)、在特定索引处获取项,等等。到目前为止,出于本代码的目的,File只有一个名称,并且包含在Directory实例中。MyModel还包含一个名为invisibleTopLevelDirectory()的方法,该方法返回作为此层次结构根的目录。

问题是我的QColumnView没有正确地显示数据。每一列最多显示一行,不管它应该有多少行。我不知道为什么!据我所知,我正确地实现了我的QAbstractItemModel派生类。更令人困惑的是,如果我使用QTreeView而不是QColumnView,树视图会显示所有数据。

以下是相关代码:

代码语言:javascript
复制
// Returns the instance of Directory at the given index, or NULL if the data
// is not a Directory instance.
Directory *directoryAtModelIndex(const QModelIndex &index)
{
    return qobject_cast<Directory *>((QObject *)index.internalPointer());
}

// Returns the instance of File at the given index, or NULL if the data
// is not a File instance.
File *fileAtModelIndex(const QModelIndex &index)
{
    return qobject_cast<File *>((QObject *)index.internalPointer());
}

QModelIndex MyModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent)) {
        return QModelIndex();
    }

    Directory *parentDirectory;

    if (!parent.isValid()) {
        parentDirectory = invisibleTopLevelDirectory();
    } else {
        parentDirectory = (Directory *)(parent.internalPointer());
    }

    if (!parentDirectory) {
        return QModelIndex();
    }

    QObject *item = parentDirectory->itemAtIndex(row);

    if (item) {
        return createIndex(row, column, item);
    } else {
        return QModelIndex();
    }
}

QModelIndex MyModel::parent(const QModelIndex &index) const
{
    if (!index.isValid()) {
        return QModelIndex();
    }

    QObject *item = (QObject *)index.internalPointer();
    Directory *parentItem = qobject_cast<Directory *>(item->parent());

    if (!parentItem || parentItem == invisibleTopLevelDirectory()) {
        return QModelIndex();
    }

    int listIndex = parentItem->indexOfItem(item);

    if (listIndex < 0) {
        return QModelIndex();
    } else {
        return createIndex(listIndex, index.column(), parentItem);
    }
}

int MyModel::rowCount(const QModelIndex &parent) const
{
    if (parent.column() > 0) {
        return 0;
    }

    if (!parent.isValid()) {
        return invisibleTopLevelDirectory() ? invisibleTopLevelDirectory()->itemCount() : 0;
    }

    Directory *parentItem = directoryAtModelIndex(parent);

    if (parentItem) {
        return parentItem->itemCount();
    } else {
        return 0;
    }
}

int MyModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 1;
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    if (role == Qt::DisplayRole) {
        if (Directory *item = directoryAtModelIndex(index)) {
            return item->name;
        } else if (File *item = fileAtModelIndex(index)) {
            return item->name;
        }   
    }

    return QVariant();
}

我已经多次讨论了这段代码,我根本不明白为什么我的QColumnView不能为任何给定的列显示多于一行的数据。我使用print语句来确认对第一个传递的行调用index()和data(),对所有父项调用rowCount(),并返回正确的行数,而父()返回正确的行、列和父QModelIndex。如果我在QTreeView中使用相同的模型,它就能正确工作,这一点尤其证实了这一点。

有人能查出我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-16 10:51:51

  1. 方法父级中的错误:而不是父行,而是使用行索引。应: 目录* parent_of_parent = qobject_cast(parentItem->parent());if (!parent_of_parent) {返回QModelIndex();} int listIndex =父_of_ parent_of_parent->indexOfItem(parentItem);
  2. 您可以使用QFileSystemModel。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37246341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档