无法让 QColumnView 使用我的 QAbstractItemModel 派生类显示多行数据
Can't get QColumnView to display more than one row of data with my QAbstractItemModel-derived class
我正在使用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中使用相同的模型,它会正确工作,这一事实尤其证实了这一点。
有人能猜出我做错了什么吗?
-
方法父级出错:您采用的不是父级的行,而是索引的行。应为:
Directory* parent_of_parent = qobject_cast<Directory*>(parentItem->parent()); if (!parent_of_parent) { return QModelIndex(); } int listIndex = parent_of_parent->indexOfItem(parentItem);
- 您可以使用QFileSystemModel
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么使用 "this" 指针调用派生成员函数?
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 在派生函数中指定void*参数
- 如何通过派生类函数更改基类中的向量
- 如何委托派生类使用其父构造函数?
- 程序崩溃并显示"std::out_of_range"错误
- 如何使用单独文件中的派生类访问友元函数对象
- 派生类销毁的最佳实践是什么
- 如何在所有者描述的 MFC CMenu 派生菜单中显示复选框?
- 为什么当我有两个函数时编译器没有显示错误,一个将采用基类,一个将派生类作为参数
- 无法让 QColumnView 使用我的 QAbstractItemModel 派生类显示多行数据
- CPropertyPage 派生对话框在显示为独立对话框时不会在 Esc 上关闭
- 当显示为对话框时,派生的Widget不以父级为中心
- Qt将子项添加到QGraphicsScene中的QGraphicsItem派生项不会显示
- 如何捕获一般异常并显示其派生的 what()