Results 1 to 2 of 2

Thread: Using boundingRegion instead of boundingRect

Hybrid View

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

    Default Using boundingRegion instead of boundingRect

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using boundingRegion instead of boundingRect

    Collisions are checked against shape() and not boundingRect(). Whatever you do with the granularity, it will not change anything until you reimplement shape() for your item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. boundingRect problems
    By mefalo in forum Qt Programming
    Replies: 3
    Last Post: 8th December 2010, 12:55
  2. Why must I pad boundingRect() by 7 pixels
    By xenome in forum Qt Programming
    Replies: 3
    Last Post: 10th September 2009, 14:07
  3. boundingRect()?
    By aaron in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2009, 09:43
  4. setRect vs boundingRect
    By zgulser in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2009, 00:23
  5. QGraphicsItem -> boundingRect()
    By harakiri in forum Qt Programming
    Replies: 4
    Last Post: 6th March 2008, 15:02

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
  •  
Qt is a trademark of The Qt Company.