- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个关于QAbstractTableModel
的底层数据源实现的一般性问题。
QAbstractTableModel::data()
函数通过由行值和列值组成的索引访问数据源内容。
如果我的基础数据源是 Person
类的 QList
,其中每个成员代表一个列,我如何使用给定的列索引访问其成员?
我能想到的唯一方法是使用一种映射,将列号映射到 Person
类的成员:
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
Person person = mySource[index.row()];
if (index.column() == 0)
return person.getName();
else if (index.column() == 1)
return person.getAdress();
(...and so on..)
}
return QVariant();
}
这是解决这个问题的方法还是有更好的方法?如果我的类(class)有 50 名成员,那将会有很多工作要做。当我通过 QAbstractTableModel::setData()
将数据写入源时,必须做同样的事情。
非常感谢能帮助我理解 Qt 中模型/ View 实现的这一部分的答案或 Material 链接。
最佳答案
Is this the approach to tackle this problem or is there a better one?
当然是一种有效的方法,是的。即使使用 QMetaObject 也没有自省(introspection)功能.
If my class has 50 members that would be a lot of work to do. The same thing must be done, when I write data to the source via QAbstractTableModel::setData().
好吧,您不太可能获得 50 个不同的成员,但在那种情况下,管理您的类(class)本来就很复杂。
Answers or links to material that would help me to understand this part of the model/view implementation in Qt are very much appriciated.
这里没有太多链接,真的。有关详细信息,请参见上文。
关于c++ - 将 QAbstractTableModel 实现与自定义类的 QList 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19205611/
试图更多地了解 QAbstractTableModel,我遇到了 rowCount 和 columnCount 的虚拟方法,这些方法在继承 QAbstractTableModel 时需要实现。 拿 i
出于测试目的,有一个函数要求输入并将其显示在 QListView 中。 display data . 这适用于一个 dim.data 我想扩展脚本显示二维数据 传入的数据应该插入第一行直到第 3 列,
最近又拿起了Qt,开始刷新内存了。为表创建自定义数据模型非常简单。 现在我正在尝试检索选定的数据。请注意,我使用自定义数据对象。 我的自定义模型示例: 平台.h class Platform { pu
如何使 QAbstractTableModel 的数据可检查 我想让下面代码中的每个单元格都可以被用户选中或取消选中,如何修改代码? 根据Qt文档:Qt::CheckStateRole和设置Qt::I
我用通常的方式实现了 QAbstractTableModel: class PrintIntervalTableModel : public QAbstractTableModel { private
我需要用 Qt 实现一个表。 我相信我会起诉一个 QAbstractTableModel,一个 QTableView 使用这个模型。 我知道我必须编辑模型的 rowCount()、columnCoun
我正在尝试显示来自 QAbstractTableModel 的图像。我尝试返回一个QPixmap作为data()的QVariant,但它只产生空单元格,而我期望第二个单元格中的每个单元格列有一个 20
我有一个使用 Q_GLOBAL_STATIC 实现的单例类,它包含一个必须从多个线程访问的数据结构,我在类中实现了访问器函数,它会在访问数据之前锁定互斥量,这样所有对共享数据的访问都是序列化的。 问题
我目前正在尝试制作一个基于QTableView 的vie。因此,我使用 QAbstractTableModel 来存储数据。我的问题是,一旦我从 QVariant headerData(int sec
我有一个子类 class TableModel : public QAbstractTableModel 我重写了 headerData 方法如下: QVariant TableModel::head
我从 QAbstractTableModel 派生了一个模型,现在我想通知一整行的数据已被更改。例如,如果更改了索引为 5 的行的数据(4 列),则使用以下代码将按预期工作。 emit dataCha
我有一个 PyQt QTableView,连接到一个 QAbstractTableModel,它本身连接到一个管理项目列表的自定义类。我可以在列表末尾插入一个项目,它会适本地通知我的模型,然后调用 b
我发现了这个问题:How to change the background color for a QTreeView Header (aka QHeaderView)? 我希望能够为每个标题部分设置
我创建了一个名为 tableModel 的 QAbstractTableModel 模型和一个名为 listModel 的 QAbstractListModel 模型,并将这些模型设置为各自的 Vie
我需要一个能够快速添加和显示大约 100'000 个项目的表格 GUI 控件。出于这个原因,需要“虚拟”表控件,它只加载和显示可见的项目。我尝试了 QTableView 和 QAbstractTabl
QTableView 由QAbstractTableModel 控制。它是填充其内容的模型。模型修改了 self.items 变量,结果是用户双击了 QTableView 的一个项目,然后输入了一个新
这是我在这里的第一篇文章,但自从我开始编程以来,我得到了你们所有人的很多帮助。 我是 Qt 的新手,目前正在尝试制作我的第一个项目。我的问题是关于模型、底层数据和 View 之间的通信。 我首先将后台
在我的应用程序中,我有一个用于保存项目列表的类: class Database : public QObject { Q_OBJECT public: Database(QObject
Qt.AlignRight 右对齐文本但将其放在右上角。 Qt.AlignRight | Qt.AlignVCenter 不起作用。将其放入左上角。 有没有办法让文本同时保持垂直居中和右对齐? 代码示
我有自己的表格模型 (DragDropTableModel),它是 QAbstractTableModel 的子类。在它里面,我希望最后一行始终保持空白。 我通过将 insertRows() 添加到
我是一名优秀的程序员,十分优秀!