无法使用我的 QAbstractItemModel 派生的 QColumnView 显示多行数据 class
Can't get QColumnView to display more than one row of data with my QAbstractItemModel-derived class
我正在使用 QAbstractItemModel 实现自定义模型,我希望它显示在 QColumnView 中。模型中的数据是目录和文件的简单层次结构,其中每个目录可以包含文件和其他目录。
我的 QAbstractItemModel 派生 class 称为 MyModel。它依赖于另外两个 QObject 派生的 classes,Directory 和 Item。 Directory 有一些方法可以返回它包含的项数(包括子目录)、获取特定索引处的项等。到目前为止,就此代码而言,File 只有一个名称并且包含在 Directory 的实例中。 MyModel 还包含一个名为 invisibleTopLevelDirectory() 的方法,该方法 returns 作为此层次结构根目录的目录。
问题是我的 QColumnView 没有正确显示数据。每列最多显示一行,不管它应该有多少行。我不知道为什么!据我所知,我已经正确地实现了我的 QAbstractItemModel 派生 class。更令人困惑的是,如果我使用 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。
我正在使用 QAbstractItemModel 实现自定义模型,我希望它显示在 QColumnView 中。模型中的数据是目录和文件的简单层次结构,其中每个目录可以包含文件和其他目录。
我的 QAbstractItemModel 派生 class 称为 MyModel。它依赖于另外两个 QObject 派生的 classes,Directory 和 Item。 Directory 有一些方法可以返回它包含的项数(包括子目录)、获取特定索引处的项等。到目前为止,就此代码而言,File 只有一个名称并且包含在 Directory 的实例中。 MyModel 还包含一个名为 invisibleTopLevelDirectory() 的方法,该方法 returns 作为此层次结构根目录的目录。
问题是我的 QColumnView 没有正确显示数据。每列最多显示一行,不管它应该有多少行。我不知道为什么!据我所知,我已经正确地实现了我的 QAbstractItemModel 派生 class。更令人困惑的是,如果我使用 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。