无法通过QAbstractItemModel派生类使QColumnView显示多行数据
我正在使用QAbstractItemModel实现我想在QColumnView中显示的自定义模型。模型中的数据是目录和文件的简单层次结构,其中每个目录可以包含文件和其他目录。无法通过QAbstractItemModel派生类使QColumnView显示多行数据
我的QAbstractItemModel派生类被称为MyModel。它依赖于另外两个QObject派生类,Directory和Item。目录有一些方法用于返回它包含的项目数(包括子目录),获取特定索引处的项目等等。就目前为止的代码而言,File只有一个名称,并且包含在Directory的实例中。 MyModel还包含一个名为invisibleTopLevelDirectory()的方法,该方法返回作为此层次结构根目录的Directory。
问题是我的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中使用这个相同的模型,它可以正常工作,这一点特别证实了这一点。
任何人都可以弄清楚我做错了什么吗?
-
父方法错误:而不是父行的行你索引的行。应该是:
Directory* parent_of_parent = qobject_cast<Directory*>(parentItem->parent()); if (!parent_of_parent) { return QModelIndex(); } int listIndex = parent_of_parent->indexOfItem(parentItem);
- 您可以使用QFileSystemModel。
你是对的!我误解了Qt关于如何实现父方法的例子。至于使用QFileSystemModel,我实际上并没有制作一个代表文件系统的模型。我的项目中的实际代码是相同的,除非名称更改为不再专有。顺便说一下,这样的错误是为什么在深夜编程时睡觉很重要。 – GuyGizmo