Hi,

I am using QtConcurrent for a function to process number of objects in batch.
For e.g. I have 1000 graphics objects, I call process() for 500 items and another call for remaining 500 items using two calls of QtConcurrent::run(...).
I get good performance improvement here. But after process() function I use these processed items to populate items in QTreewidget.
The time required to populate the QTreeWidget tremendously increases as compared to the time required for populating before we used QtConcurrent.


I have a XML file which contains details of each of these items which includes id, position, height, width. My application reads this file and pass this detail to process() to creates the Item class objects.

Qt Code:
  1. class Item :: QGraphicsObject
  2. {
  3. QString m_sId;
  4. int m_iHeight, m_iWidth;
  5. QPoint m_position;
  6. };
  7.  
  8. // process()
  9. process(int iStart, int iEnd, <list of XMLNode>)
  10. {
  11. for(iStart to iEnd)
  12. {
  13. // Read XMLNode and create Item object using the read details
  14. }
  15. }
  16.  
  17. // read()
  18.  
  19. read()
  20. {
  21. // Read the XML File
  22. QList<XMLNode> lstNodes = readXML();
  23.  
  24. // call process() for list of XMLNode
  25. QList<Item*> lstItems = process(1, lstNodes.size(), lstNodes);
  26.  
  27. // Add these items to scene using m_scene->addItem((QGraphicsItem*) item);
  28.  
  29. // Iterate through the list of items and add each of the item to QTreeWidget
  30. for(i = 0 to size )
  31. {
  32. Item* item = lstItems1.at(i);
  33. QTreeWidgetItem *treeItem = new QTreeWidgetItem(Items);
  34. treeItem->setText(0, tr(item->m_sId));
  35. }
  36. }
To copy to clipboard, switch view to plain text mode 

Below is the modified code using QtConcurrent::run()

Qt Code:
  1. // process()
  2. QList<Item*> process(int iStart, int iEnd, <list of XMLNode>)
  3. {
  4. QList<Item*> lstItems;
  5. for(iStart to iEnd)
  6. {
  7. // Read XMLNode and create Item object using the read details
  8. // Move the created graphics object on main thread as it needs to be added to scene later
  9. item.moveToThread(QApplication::instance()->thread());
  10. }
  11. return lstItems;
  12. }
  13.  
  14. read()
  15. {
  16. // Read the XML File
  17. QList<XMLNode> lstNodes = readXML();
  18.  
  19. // call process() for list of XMLNode
  20. int iSize = lstNodes.size();
  21.  
  22. QFutureSynchronizer<QList<Item*>> synchronizer;
  23.  
  24. QFuture<QList<Item*>> future1 = QtConcurrent::run(this, &Reader::process, 1, iSize/2, lstNodes);
  25. QFuture<QList<Item*>> future2 = QtConcurrent::run(this, &Reader::process, iSize/2, iSize, lstNodes);
  26.  
  27. synchronizer.addFuture(future1);
  28. synchronizer.addFuture(future2);
  29.  
  30. synchronizer.waitForFinished();
  31.  
  32. QList<Item*> lstItems1 = future1.result();
  33. QList<Item*> lstItems2 = future2.result();
  34.  
  35. lstItems1.append(lstItems2);
  36.  
  37. // Add these items to scene using m_scene->addItem((QGraphicsItem*) item);
  38.  
  39. // Iterate through the list of items and add each of the item to QTreeWidget
  40. for(int i = 0; i<= size; i++ )
  41. {
  42. Item* item = lstItems1.at(i);
  43. QTreeWidgetItem *treeItem = new QTreeWidgetItem(treeWidget);
  44. treeItem->setText(0, tr(item->m_sId));
  45. }
  46. }
To copy to clipboard, switch view to plain text mode 

My problem is, previously populating the TreeWidget was taking less time (1.9 sec) for 1000 items but now when I used QtConcurrent for process(), it has been increased (21.5 sec) whereas calling process() using QtConcurrent takes less time.

Can you please guide me resolving this issue?