Results 1 to 2 of 2

Thread: Speeding up dense collection of QGraphicsItems

  1. #1
    Join Date
    Nov 2010
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Speeding up dense collection of QGraphicsItems

    Hello,

    I'm trying to display a dense vector field (at times >10,000 vectors) over an image. My primary problem is that when scrolling across the image, the performance is quite laggy and the drawing time is quite substantial. I've looked online for a number of qgraphicsscene optimizations, but I haven't noticed too big of a performance improvement. Perhaps I'm missing something quite minor, I'm pretty new at using these graphics objects. I've attached images of the display. The current drawing time is around 300 msec.

    I've implemented each of these as a QGraphicsItem. The class is given below:
    Qt Code:
    1. VectorItem::VectorItem(const qpiv_vector &vec,
    2. const qpiv_settings &settings,
    3. int idx_i)
    4. {
    5. idx = idx_i;
    6. scale_max = settings.scale_max;
    7. snr = vec.snr;
    8. vec_color_type = settings.vec_color_type;
    9. highlight_flag = settings.highlight_flag;
    10. interp = vec.interp;
    11.  
    12. dmag = sqrt( pow(vec.dx-settings.subtract_x,2) + pow(vec.dy-settings.subtract_y,2) );
    13.  
    14. x2 = dmag*settings.vec_scale_factor;
    15. y2 = 0;
    16.  
    17. upper_x = x2 - 0.15*x2;
    18. lower_x = upper_x;
    19. upper_y = y2 + 0.07*x2;
    20. lower_y = y2 - 0.07*x2;
    21.  
    22. this->setPos(vec.x,vec.y);
    23. this->setRotation(atan2(vec.dy-settings.subtract_y,vec.dx-settings.subtract_x)*180/M_PI);
    24. this->setAcceptHoverEvents(true);
    25.  
    26. // Pen definition.
    27. findColor();
    28. textPen.setWidth(2);
    29. textPen.setCapStyle(Qt::RoundCap);
    30. textPen.setColor(color);
    31.  
    32. // BoundingRect definition.
    33. points.resize(3);
    34. points[0] = QLineF(0,0,x2,y2);
    35. points[1] = QLineF(upper_x,upper_y,x2,y2);
    36. points[2] = QLineF(lower_x,lower_y,x2,y2);
    37.  
    38. // Shape definition.
    39. points2.resize(8);
    40. points2[0] = QPointF(-1,-1);
    41. points2[1] = QPointF(-1,1);
    42. points2[2] = QPointF(upper_x -1, 1);
    43. points2[3] = QPointF(upper_x -1, upper_y +1);
    44. points2[4] = QPointF(x2 + 1 , 0);
    45. points2[5] = QPointF(lower_x -1, lower_y -1);
    46. points2[6] = QPointF(lower_x -1, -1);
    47. points2[7] = QPointF(-1,-1);
    48. QPolygonF polygon(points2);
    49. path.addPolygon(polygon);
    50.  
    51. }
    52.  
    53. QRectF VectorItem::boundingRect() const {
    54. return QRectF(-x2-2,-x2-2,2*x2+2,2*x2+2);
    55. }
    56.  
    57. QPainterPath VectorItem::shape() const {
    58. return path;
    59. }
    60.  
    61. void VectorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    62. QWidget *widget)
    63. {
    64. painter->setPen(textPen);
    65. painter->drawLines(points);
    66. }
    67.  
    68. void VectorItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event ) {
    69. textPen.setWidth(4);
    70. this->update(this->boundingRect());
    71. }
    72.  
    73. void VectorItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) {
    74. textPen.setWidth(2);
    75. this->update(this->boundingRect());
    76. }
    77.  
    78. void VectorItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) {
    79. openInfoDialog(idx);
    80. }
    81.  
    82. void VectorItem::findColor() {
    83. int idx;
    84. if (vec_color_type == COLOR_VMAG) {
    85. idx = floor((dmag/scale_max)*COLORMAP_SIZE);
    86. } else if (vec_color_type == COLOR_SNR) {
    87. idx = floor(((snr-0.5)/2.5)*COLORMAP_SIZE);
    88. }
    89.  
    90. if (idx > COLORMAP_SIZE) {
    91. idx = COLORMAP_SIZE;
    92. }
    93.  
    94. if ((highlight_flag == true) && (interp == false)) {
    95. color = Qt::white;
    96. } else {
    97. color = qRgb(jet_colormap[idx][1],jet_colormap[idx][2],jet_colormap[idx][3]);
    98. }
    99. }
    To copy to clipboard, switch view to plain text mode 

    And the implementation of the class:
    Qt Code:
    1. for (int idx = 0; idx < grid.n; idx++) {
    2. VectorItem *newarrow =
    3. new VectorItem(vec.at(idx),settings,idx);
    4. connect(newarrow,SIGNAL(openInfoDialog(int)),this,SLOT(openInfoDialog(int)));
    5. scene->addItem(newarrow);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated!!!
    Attached Images Attached Images

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Speeding up dense collection of QGraphicsItems

    If you haven't done so already, try fiddling with the QGraphicsView optimization flags.

    If that fails, one idea would be to replace your thousands of QGraphicsItems with a single one that handles the painting of all vectors. This eliminates the overhead each individual QGraphicsItem carries with it, both in terms of memory and execution, but you lose some of the convenience of individual objects. If items are spaced on a regular grid, though, this may be acceptable.

Similar Threads

  1. Replies: 2
    Last Post: 8th December 2010, 10:51
  2. Garbage collection
    By Septi in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 14:13
  3. Speeding up Image Rendering
    By reno77 in forum Newbie
    Replies: 2
    Last Post: 15th June 2010, 09:58
  4. lib using Image Collection
    By bitChanger in forum Qt Programming
    Replies: 4
    Last Post: 25th July 2006, 17:30
  5. Speeding up QPaint
    By georgie in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2006, 02:06

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.