Results 1 to 1 of 1

Thread: Create QGrapchisObject in sub QThread, but not Efficiency promotion

  1. #1
    Join Date
    Jul 2015
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question Create QGrapchisObject in sub QThread, but not Efficiency promotion

    Hi, Everyone.
    I am using QGraphicsView system to show a layout in chip design system.(EDA tools like Virtuso、Thunder、ICC)
    And the max amount of QGraphicsObject/QGraphicsItem can be 500,000+, or even more.
    Thus i have problem making items easy and quick to be drawn or view in the scene.
    I followed the Chips demo, and query the solutions from Google, also read lots of topics in QtCentre.
    But still i did not find a viable solution, Also Text item impact enormous.
    Out group use the tree structure to organize the objects, which use a root item as the top level item, and then level 2, level 3...
    But the users always need to flat all the items. And Then i use QTransform()::m11() to control the shape the item is paint.
    But with so many items inside even this methods does not work.

    There are two difficult parts on this case:

    1)how to reduce the loading time for creating QGraphicsItem.
    2)how to make it smoother while zoom in/zoom out, with so many items.


    Codes below are the sample i tried to use QThread to save loading time. But it does not work, or the program works
    bad than no sub thread is used to load the items. I am very curious why the program has no response when i used sub thread,
    and the program runs smoother with no sub thread.

    Thanks all.

    Qt Code:
    1. // Used to as Root item
    2. class CRoot: public QGraphicsObject
    3. {
    4. public:
    5. CRoot(QGraphicsObject *opParent = NULL) : QGraphicsObject(opParent)
    6. {}
    7.  
    8. QRectF boundingRect() { return QRectF(); }
    9. ...
    10. };
    11.  
    12. // Leaf Item
    13. class Chip : public QGraphicsObject
    14. {
    15. ...
    16. void paint(QPainter *opPainter, const QStyleOptionGraphicsItem *opOPtion, QWidget *opWidget)
    17. {
    18. Q_UNUSED(opOPtion);
    19. Q_UNUSED(opWidget);
    20. opPainter->save();
    21. double d_value = mdWidth * transform().m11();
    22. if (d_value >= 7)
    23. {
    24. opPainter->drawShape(moShape);
    25. }
    26. else
    27. {
    28. opPainter->drawPoint(0, 0);
    29. }
    30.  
    31. opPainter->restore();
    32. }
    33.  
    34. private:
    35. QPainterPath moShape; // used to store the path of Chip
    36. QGraphicsTextItem *mopLabel; // Label item of this cell.
    37. };
    38.  
    39. class CLayout : public QGraphicsObject
    40. {
    41. Q_OBJECT
    42. public:
    43. CLayout(QGraphicsObject *opParent = NULL) : QGraphicsObject(opParent)
    44. {
    45.  
    46. }
    47.  
    48. CRoot *mopGetRoot()
    49. {
    50. QReadLocker o_locker(&moLock);
    51. return mopRoot;
    52. }
    53. signals:
    54. void msigLoadDone();
    55.  
    56. public slots:
    57. void mslotLoadItems()
    58. {
    59. QWriteLocker o_locker(&moLock);
    60. mopRoot = new CRoot;
    61.  
    62. const int I_COUNT = 500000;
    63. for (int i_cnt = 0; i_cnt < I_COUNT; ++i_cnt)
    64. {
    65. Chip *op_chip = new chip(CRoot);
    66. op_chip->setPos(i_cnt / 10000, i_cnt / 10000);
    67. mlstopItems.push_back(op_chip);
    68. }
    69.  
    70. // move items from curThread to GUI thread.
    71. moveToThread(QApplication::instance()->thread());
    72. mopRoot->moveToThread(QApplication::instance()->thread());
    73. foreach(Chip *op_item, mlstopItems)
    74. {
    75. op_item->moveToThread(QApplication::instance()->thread());
    76. }
    77.  
    78. emit msigLoadDone();
    79. }
    80.  
    81. private:
    82. CRoot *mopRoot;
    83. QList<Chip *> mlstopItems;
    84. };
    85.  
    86. class CMainWindow: public QMainWindow
    87. {
    88. Q_OBJECT
    89. public:
    90. ...
    91. void mvInitScene()
    92. {
    93. mopScene = new QGraphicsScene();
    94. }
    95.  
    96. signals:
    97. void msigGoToLoad();
    98.  
    99. public slots:
    100. void mslotSetRootItem()
    101. {
    102. std::call_once(meFlag, &CMainWindow::mvInitScene, this);
    103. mopScene()->addItem(mopLayout->mopGetRoot());
    104. }
    105.  
    106. void mslotBtnClicked()
    107. {
    108. mopThread = new QThread();
    109. mopLayout = new Layout();
    110.  
    111. #ifdef USED_THREAD
    112. connect(this, SIGNAL(msigGoToLoad()), mopLayout, SLOT(mslotLoadItems()));
    113. connect(mopLayout, SIGNAL(msigLoadDone()), this, SLOT(mslotSetRootItem()));
    114. connect(mopThread, SIGNAL(finished()), mopThread, SLOT(deleteLater()));
    115. mopLayout->moveToThread(mopThread);
    116. mopThread->start();
    117. #else
    118. mopLayout->mslotLOadItems();
    119. mslotSetRootItem();
    120. #endif
    121. emit msigGoToLoad();
    122. }
    123. private:
    124. QThread *mopThread;
    125. CLayout *mopLayout;
    126. QGraphicsScene *mopScene;
    127. std::once_flag meFlag;
    128. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by cybfly; 6th July 2015 at 15:13.

Similar Threads

  1. Widget Promotion
    By A9am in forum Newbie
    Replies: 4
    Last Post: 6th February 2013, 11:39
  2. QWidget promotion with child widgets
    By Phlucious in forum Qt Programming
    Replies: 3
    Last Post: 22nd February 2012, 07:41
  3. promotion problems with designer
    By szisziszilvi in forum Qt Programming
    Replies: 3
    Last Post: 26th April 2011, 08:27
  4. Replies: 3
    Last Post: 18th February 2010, 15:01
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.