无法让 QColumnView 使用我的 QAbstractItemModel 派生类显示多行数据

Can't get QColumnView to display more than one row of data with my QAbstractItemModel-derived class

本文关键字:派生 显示 数据 QAbstractItemModel 我的 QColumnView      更新时间:2023-10-16

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

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

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

以下是相关代码:

// 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()是对所有父项调用的,并返回正确的行数,parent()返回正确的列、行和父QModelIndex。如果我在QTreeView中使用相同的模型,它会正确工作,这一事实尤其证实了这一点。

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

  1. 方法父级出错:您采用的不是父级的行,而是索引的行。应为:

    Directory* parent_of_parent = qobject_cast<Directory*>(parentItem->parent());
    if (!parent_of_parent) {
        return QModelIndex();
    }
    int listIndex = parent_of_parent->indexOfItem(parentItem);
    
  2. 您可以使用QFileSystemModel