【发布时间】:2011-05-08 09:29:25
【问题描述】:
在我的程序中,我想将视图/模型模式与 view = QListView 和我自己的模型(我从 QAbstractListModel 子类化)一起使用。我的数据类看起来像
class Avtomat
{
...
map<QString, State *> states;
...
};
在我的模型类中
class AvtomatModel : public QAbstractListModel
{
...
Avtomat a;
...
};
我正在尝试重载 QAbstractItemView::index 函数,以便能够提供用于编辑数据映射的界面。 由于索引函数采用 int row 参数,我通过提供以下内容解决了该问题
State* Avtomat::pStateFromIndex(int index) const
{
map<QString, State *>::const_iterator i;
int count = 0;
for (i = states.begin(); i != states.end() && count != index; ++i)
++count;
return (*i).second;
}
所以在我的索引函数中我确实喜欢这样
return createIndex(row, column, a.pStateFromIndex(row));
但这看起来很丑,因为我有 O(n)。你能帮我设计一个更好的方法来使用 int index 访问我的地图吗?
【问题讨论】: