Вообщем нужно отбразить таблицу из Lua в QTreeView
Пример таблици
Lua
testtableret={
{["name"]="giud",value=10282},
{["name"]="count",value=5},
{["name"]="Item 50365",["value"]=
{
{["name"]="Slot",["value"]=1},
{["name"]="Entry",["value"]=50365},
{["name"]="DisplayId",["value"]=0},
{["name"]="ItemsCount",["value"]=0},
{["name"]="Price",["value"]=0},
{["name"]="MaxDurability",["value"]=0},
{["name"]="BuyCount",["value"]=0},
{["name"]="ExtendedCostId",["value"]=0},
}
},
{["name"]="Item 50367",["value"]=
{
{["name"]="Slot",["value"]=2},
{["name"]="Entry",["value"]=50367},
{["name"]="DisplayId",["value"]=0},
{["name"]="ItemsCount",["value"]=0},
{["name"]="Price",["value"]=0},
{["name"]="MaxDurability",["value"]=0},
{["name"]="BuyCount",["value"]=0},
{["name"]="ExtendedCostId",["value"]={{["name"]="Test",["value"]=5},{["name"]="Tes1",["value"]= {
{["name"]="Slot",["value"]=1},
{["name"]="Entry",["value"]=50365},
{["name"]="DisplayId",["value"]=0},
{["name"]="ItemsCount",["value"]=0},
{["name"]="Price",["value"]=0},
{["name"]="MaxDurability",["value"]=0},
{["name"]="BuyCount",["value"]=0},
{["name"]="ExtendedCostId",["value"]=0},
}}}},
}
},
}
Делаю на основе http://doc.trolltech.com/4.5/itemviews-simpletreemodel.html
http://doc.trolltech.com/4.5/itemviews-simpletreemodel-treeitem-cpp.html Без изменений.
Функция получает список Имя,Obj
C++ (Qt)
void MainWindow::ToObjList(QList<PacketParser::objects>* objlist,luabind::object obj)
{
for(luabind::iterator iter(obj), end; iter != end; ++iter) {
if(luabind::type(*iter)==LUA_TTABLE)
{
PacketParser::objects* tmp=new PacketParser::objects();
for(luabind::iterator it(*iter), end; it != end; ++it) {
if(luabind::type(*it)==LUA_TSTRING)
tmp->name=new QString(luabind::object_cast<const char *>(*it));
else
tmp->value=*it;
}
objlist->append(*tmp);
}
}
}
Передаем список в модель
C++ (Qt)
QList<PacketParser::objects> objlist;
ToObjList(&objlist,PP.GetTable());
model=new TreeModel(&objlist);
ui->treeView->setModel(model);
И собственно говоря основной код
C++ (Qt)
void TreeModel::setupModelDataRec(luabind::object obj,QList<TreeItem*> &parent,bool fromTbl)
{
QList<QVariant> columnData;
int hastable=1;
for(luabind::iterator iter(obj), end; iter != end; ++iter)
{
//qDebug(tr("%1").arg(luabind::type(*iter)).toAscii());
switch(luabind::type(*iter))
{
case LUA_TNIL:
columnData<<"nil";
break;
case LUA_TBOOLEAN:
if(obj) { columnData<< "true"; }
else { columnData<< "false"; }
break;
case LUA_TNUMBER:
columnData<<luabind::object_cast<double>(*iter);
break;
case LUA_TSTRING:
columnData<<luabind::object_cast<const char *>(*iter);
break;
case LUA_TTABLE:
{
hastable=0;
if(!fromTbl) parent << parent.last()->child(parent.last()->childCount()-1);
if(!columnData.isEmpty())
parent.last()->appendChild(new TreeItem(columnData, parent.last()));
if(!fromTbl) setupModelDataRec(*iter,parent,true); else setupModelDataRec(*iter,parent,false);
if(!fromTbl) parent.pop_back();
}
break;
}
}
if(hastable) parent.last()->appendChild(new TreeItem(columnData, parent.last()));
}
void TreeModel::setupModelData(QList<PacketParser::objects>* objlist, TreeItem *parent)
{
QList<TreeItem*> parents;
QList<int> indentations;
parents << parent;
indentations << 0;
QList<PacketParser::objects>::iterator i = objlist->begin();
while (i != objlist->end()) {
QList<QVariant> columnData;
columnData<<*(*i).name;
switch(luabind::type((*i).value))
{
case LUA_TNIL:
columnData<<"nil";
break;
case LUA_TBOOLEAN:
if((*i).value) { columnData<< "true"; }
else { columnData<< "false"; }
break;
case LUA_TNUMBER:
columnData<<luabind::object_cast<double>((*i).value);
break;
case LUA_TSTRING:
columnData<< luabind::object_cast<const char *>((*i).value);
break;
}
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
if (luabind::type((*i).value)==LUA_TTABLE)
setupModelDataRec((*i).value,parents);
++i;
}
}
Больше всего костылей в setupModelDataRec
может есть кто подскажет?