Hello,

I'm trying to implement some hovering commands on a quiver plot I've made using a QGraphicsItem. I would like the arrows to be highlighted when the mouse is over them, but I'm running into issues because a rectangular boundingRect() causes overlap sometimes. Because of this I'd like to use boundingRegion() with a high granularity, however I am unsure how to implement this in my class. I've looked all around for examples of its use but I havent found too many. How is boundingRegion() used instead of boundingRect? Thanks for all the help!!

Here is a snippet of the code I have for the items:

Qt Code:
  1. class SimpleItem : public QGraphicsItem
  2. {
  3. public:
  4.  
  5. qreal dx, dy;
  6. qreal subtract_x, subtract_y;
  7. qreal scale_factor;
  8. bool test;
  9. bool highlight_flag;
  10. qint32 vector_color_type;
  11. qreal snr;
  12. qreal scale_max;
  13. QPen textPen;
  14. QColor color;
  15. qreal dmag;
  16.  
  17. SimpleItem(qreal x_i, qreal y_i, qreal dx_i, qreal dy_i,
  18. qreal subtract_x_i, qreal subtract_y_i,
  19. qreal snr_i, bool test_i,
  20. qreal scale_factor_i, qint32 vector_color_type_i,
  21. qreal scale_max_i,
  22. bool highlight_flag_i) {
  23.  
  24. this->setPos(x_i,y_i);
  25. this->setAcceptHoverEvents(true);
  26. this->setBoundingRegionGranularity(1);
  27.  
  28. dx = dx_i;
  29. dy = dy_i;
  30.  
  31. subtract_x = subtract_x_i;
  32. subtract_y = subtract_y_i;
  33.  
  34. snr = snr_i;
  35. test = test_i;
  36.  
  37. highlight_flag = highlight_flag_i;
  38.  
  39. scale_factor = scale_factor_i;
  40. vector_color_type = vector_color_type_i;
  41.  
  42. scale_max = scale_max_i;
  43.  
  44. dmag = sqrt(pow(dx-subtract_x,2) + pow(dy-subtract_y,2));
  45. findColor();
  46.  
  47. }
  48.  
  49. QRectF boundingRect() const
  50. {
  51. return QRectF(-10,-10,20,20);
  52. }
  53.  
  54. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
  55. QWidget *widget)
  56. {
  57.  
  58. textPen = QPen(color);
  59.  
  60. qreal tangent = atan2 ((dy-subtract_y),(dx-subtract_x));
  61. qreal x2 = (dx-subtract_x)*scale_factor;
  62. qreal y2 = (dy-subtract_y)*scale_factor;
  63. qreal pa_x = x2 - (scale_factor*dmag/6) * cos (tangent + M_PI / 7);
  64. qreal pa_y = y2 - (scale_factor*dmag/6) * sin (tangent + M_PI / 7);
  65. qreal pb_x = x2 - (scale_factor*dmag/6) * cos (tangent - M_PI / 7);
  66. qreal pb_y = y2 - (scale_factor*dmag/6) * sin (tangent - M_PI / 7);
  67.  
  68. QVector<QLineF> points(3);
  69. points[0] = QLineF(0,0,x2,y2);
  70. points[1] = QLineF(pa_x,pa_y,x2,y2);
  71. points[2] = QLineF(pb_x,pb_y,x2,y2);
  72.  
  73. textPen.setWidth(2);
  74. textPen.setCapStyle(Qt::RoundCap);
  75. painter->setPen(textPen);
  76. painter->drawLines(points);
  77.  
  78. }
  79.  
  80.  
  81. };
To copy to clipboard, switch view to plain text mode