简体   繁体   English

使用QHash和QList提高性能

[英]Improving performance using QHash and QList

I'm using these classes: 我正在使用这些类:

QHash : rapresenting all the objects of the scene (cannot modify this class) QHash :代表场景的所有对象(无法修改此类)

QList : rapresenting all the objects selected. QList :代表所有选定的对象。 It contains IDs (saved as int ) 它包含ID(保存为int

//DrawSelectedObjects(){

QHash<QString, SceneObject*>& hash=sc->getObj();
QList<int> tempList = HitsList;

int counter =0;

for (QHash<QString,SceneObject*>::ConstIterator i = hash.begin();i!=hash.end();++i) {

     if (tempList.startsWith(counter)) {
            .
            Draw_as_selected()
            .
            tempList.removeOne(counter);
     }
}
}

So,for example if I select the object #77,its ID is saved in Hitslist ( QList ). 因此,例如,如果我选择对象#77,其ID将保存在HitslistQList )中。

After that HitsList is sorted and DrawSelectedObjects() is called. 之后,对HitsList进行排序并DrawSelectedObjects()

It has to iterate the QHash until counter=77 and Draw_as_selected() . 它必须迭代QHash直到counter=77Draw_as_selected() After that,The first element of the QList is removed, pulling to the front the second one. 之后,删除QList的第一个元素,将第二个元素拉到前面。

This function is called EVERY time one object is selected. 每次选择一个对象时都会调用此函数。 With small imported scenes its all ok,but when I'm using files >10MB I can see some output lag (its obvious, because I am iterating through a huge QHash ). 对于小的导入场景一切都好,但是当我使用> 10MB的文件时,我可以看到一些输出滞后(很明显,因为我正在迭代一个巨大的QHash )。

Could you suggest me a more efficient way to do this? 你能建议我一个更有效的方法吗? Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: 编辑:

Thank you for your reply.The problem is that I can't get rid of that QList<int> (iI can push only integers on top of OpenGL selection stack). 谢谢你的回复。问题是我无法摆脱那个QList<int> (iI只能在OpenGL选择堆栈之上推送整数)。

So another way from the above solution is to do QString.toInt() for every element of the QHash and save them into the QList<int> . 因此,从上述溶液中的另一种方式是做QString.toInt()的每一个元件QHash ,并将它们保存到QList<int>

The fact is...how find out the correct QString on the hash using the int (calculated now by conversion from QString , no more from counter) on the QList ? 事实是......如何在QList上使用int (现在通过QString转换计算,不再来自计数器)在哈希上找到正确的QString

If the way you access objects in the hash is using a QString (I guess the "name" of an object, instead of its ID), then you should also use a list of QString to store the selected objects. 如果您在散列中访问对象的方式是使用QString (我猜对象的“名称”而不是其ID),那么您还应该使用QString列表来存储所选对象。

QHash<QString, SceneObject*> & hash = getAllObjects();
QList<QString> & tempList = getSelectedObjects();

foreach(QString name, tempList)
    hash[name]->drawAsSelected(); // or drawAsSelected(hash[name]) depending on your design

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM