在我的程序中,我想使用視圖/模型模式與view = QListView和我從QAbstractListModel subclassed我自己的模型。我的數據類看起來像覆蓋QAbstractItemModel :: index和訪問std :: map
class Avtomat
{
...
map<QString, State *> states;
...
};
在我的模型類
class AvtomatModel : public QAbstractListModel
{
...
Avtomat a;
...
};
我試圖重載QAbstractItemView中::索引功能,使我能夠提供的數據編輯地圖界面。 作爲指數函數接受INT行參數我解決了這個問題,在我的索引功能,提供以下
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索引訪問我的地圖嗎?