将QStringListModel包装在QABSTRACTITEMMODEL中,呈现一个空白列表
Wrapping a QStringListModel in a QAbstractItemModel renders a blank list
我想开始为QT列表视图制作自己的模型,我认为我将首先将QStringListModel
包装在我自己的QAbstractItemModel
中,然后将其渲染在列表视图中。但是,它只会呈现一个空白的白色正方形,而不是我期望的列表。鉴于我要做的只是将所有电话委派给QStringListModel
,我真的不知道会发生什么。也许QListView
调用CC_5的某些方面不受QAbstractItemModel
纯虚拟方法的要求?也许它与QStringList
的存储有关?
我的尝试在下面。标题:
class DelegatingItemModel: public QAbstractItemModel {
public:
DelegatingItemModel();
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
private:
QAbstractItemModel* innerModel;
};
这是实现:
#include "delegating_item_model.hh"
DelegatingItemModel::DelegatingItemModel() {
QStringList available = {"foo", "bar", "baz"};
this->innerModel = new QStringListModel(available);
}
QVariant DelegatingItemModel::data(const QModelIndex &index, int role) const {
return innerModel->data(index, role);
}
int DelegatingItemModel::columnCount(const QModelIndex &parent) const {
return innerModel->columnCount(parent);
}
int DelegatingItemModel::rowCount(const QModelIndex &parent) const {
return innerModel->rowCount(parent);
}
QModelIndex DelegatingItemModel::parent(const QModelIndex &index) const {
return innerModel->parent(index);
}
QModelIndex DelegatingItemModel::index(int row, int column, const QModelIndex &parent) const {
return innerModel->index(row, column, parent);
}
这是入口点:
int main(int argc, char** argv) {
qDebug() << "Starting up";
QApplication app(argc, argv);
QMainWindow mainWindow;
QListView* listView = new QListView;
DelegatingItemModel* theModel = new DelegatingItemModel;
listView->setModel(theModel);
mainWindow.setCentralWidget(listView);
mainWindow.show();
return app.exec();
}
您的视图仅在给定索引链接到其模型时才从模型中获取数据。如果您在Data()方法中打印跟踪,您将看到它从未被调用。
因此,您无法返回内部列表模型创建的新索引,因为它将链接到列表,而不是您自己的模型。例如:
QModelIndex DelegatingItemModel::index(int row, int column, const QModelIndex &parent) const {
//return innerModel->index(row, column, parent);
if (parent.isValid()) // It's a list. Not a tree
return QModelIndex();
return createIndex(row, column); // Create a index for your own model.
}
要保持完整,您应该转换data()中的索引:
QVariant DelegatingItemModel::data(const QModelIndex &index, int role) const {
QModelIndex const innerIndex(innerModel->index(index.row(), index.column()));
return innerModel->data(innerIndex, role);
}
相关文章:
- 链表,将列表复制到另一个列表
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- 是否可以使用OpenMP并行化一个列表,该列表可以在每次迭代中添加新元素
- 打印一个列表给出错误,指出迭代器不可递减
- C++:从两个包含子类 (typedef) 的列表创建一个列表
- 在 std 列表中查找对象并将其添加到另一个列表
- 在c++中遍历一个列表并在另一个列表中搜索每个元素
- 我在 C++ 中创建了一个列表容器类,但它没有按预期工作
- 如何通过赋值运算符重载将一个列表复制到另一个列表上?C++
- 正在将指针复制到assign运算符中的另一个列表元素
- 在c++中传递一个列表作为返回类型
- 如何在c++中创建一个列表及其内容常量
- C++:如何将迭代器设置为另一个列表中的结构内的列表
- C++ std::sort 引用另一个列表的自定义比较函数
- 如何让数组在下一次输入之前显示数组内容的前一个列表?
- 如果我创建一个列表:<T>:迭代器,有没有办法重载++(增量)运算符?
- 我如何解码一个列表的列表
- 如何在c++中用一个目录中的所有文件夹填充一个列表框
- c++遍历一个列表来调用某个函数