Results 1 to 7 of 7

Thread: Problem with derived class from QGraphicsItem and drawing

  1. #1
    Join Date
    Jul 2012
    Location
    Switzerland
    Posts
    32
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with derived class from QGraphicsItem and drawing

    Hello everyone,

    I've tried to create an ArrowItem (derived from QGraphicsItem) which displays an Arrow.

    Qt Code:
    1. #include "arrowitem.h"
    2.  
    3. ArrowItem::ArrowItem(QGraphicsItem *parent)
    4. :QGraphicsItem(parent)
    5. {
    6. }
    7.  
    8. void ArrowItem::changeLine(QPointF start, QPointF end)
    9. {
    10. double rSquared = 25.0;
    11. //Point which lies on line which indicates begining of the Arrow
    12. // ---|>
    13. QPointF onLine = 19.0/20.0*(end-start)+start;
    14.  
    15. double m = (end.y()-start.y())/(end.x()-start.x());
    16. double mRect = -1/m;
    17. double b = onLine.y()- mRect*onLine.x();
    18. double c = qPow(mRect,2)+1.0;
    19. double d = 2.0*b*mRect -2.0*onLine.x() -2.0*mRect*onLine.y();
    20. double e = qPow(b,2) + qPow(onLine.x(),2) + qPow(onLine.y(),2) -2.0*b*onLine.y() -rSquared;
    21.  
    22. double xOne = (-d + qSqrt(d*d-4.0*c*e))/(2.0*c);
    23. double yOne = xOne*mRect + b;
    24. double xTwo = (-d - qSqrt(d*d-4.0*c*e))/(2.0*c);
    25. double yTwo = xTwo*mRect + b;
    26.  
    27. QPointF pointOne = QPointF(xOne,yOne);
    28. QPointF pointTwo = QPointF(xTwo,yTwo);
    29.  
    30.  
    31. triangle << pointOne << pointTwo << end << pointOne;
    32. line.setPoints(start, end);
    33.  
    34. double distanceX;
    35. double width;
    36. double distanceY;
    37. double height;
    38.  
    39. double left;
    40. double top;
    41.  
    42. if(start.x() < end.x())
    43. {
    44. if(pointOne.x() > pointTwo.x())
    45. {
    46. distanceX = pointOne.x();
    47. }else{
    48. distanceX = pointOne.x();
    49. }
    50. if(end.x() > distanceX)
    51. {
    52. distanceX = end.x();
    53. }
    54. width = distanceX - start.x();
    55. left = start.x();
    56. }else{
    57. if(start.x() == end.x())
    58. {
    59. width = 20.0;
    60. }else{
    61. if(pointOne.x() < pointTwo.x())
    62. {
    63. distanceX = pointOne.x();
    64. }else{
    65. distanceX = pointOne.x();
    66. }
    67. if(end.x() < distanceX)
    68. {
    69. distanceX = end.x();
    70. }
    71. width = start.x() - distanceX;
    72. left = distanceX;
    73. }
    74. }
    75.  
    76. //get height
    77. if(start.y() > end.y())
    78. {
    79. if(pointOne.y() < pointTwo.y())
    80. {
    81. distanceY = pointOne.y();
    82. }else{
    83. distanceY = pointTwo.y();
    84.  
    85. if(end.y() < distanceY)
    86. {
    87. distanceY = end.y();
    88. }
    89. }
    90. height = start.y() - distanceY;
    91. top = start.y();
    92. }else{
    93. if(start.y() == end.y())
    94. {
    95. height = 20.0;
    96. }else{
    97. if(pointOne.y() > pointTwo.y())
    98. {
    99. distanceY = pointOne.y();
    100. }else{
    101. distanceY = pointTwo.y();
    102.  
    103. if(end.y() > distanceY)
    104. {
    105. distanceY = end.y();
    106. }
    107. }
    108. height = distanceY - start.y();
    109. top = distanceY;
    110. }
    111. }
    112.  
    113. double penwidth = 3.0;
    114. prepareGeometryChange();
    115. rect = QRectF(left -penwidth/2,top -penwidth/2,width + penwidth,height + penwidth);
    116. update();
    117. }
    118.  
    119. QRectF ArrowItem::boundingRect() const
    120. {
    121. return rect;
    122. }
    123.  
    124. void ArrowItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    125. {
    126. painter->setPen(QPen(Qt::black, 3, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
    127. painter->drawLine(line);
    128.  
    129. //painter->setBrush(QBrush(Qt::black));
    130. painter->drawPolygon(triangle);
    131.  
    132. }
    To copy to clipboard, switch view to plain text mode 

    I don't really get where I've made a mistake because I used the prepareGeometrychange() method before I change the boundingRect and I've called the update() method
    If I create a new object of this class and call the method ArrowItem::changeLine with the starting Point (where the mousePressEvent happened) and end point (where the mousemoveEvent happened), there are pieces left on the screen.

    thank you very much

    airglide

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

    Default Re: Problem with derived class from QGraphicsItem and drawing

    What do you pass to changeLine()? I'm assuming you're passing two points in scene coordinates. This is not entirely correct as then boundingRect() of the line is also specified in scene coordinates instead of local item coordinates. I would suggest to treat "start" as origin of the item coordinate system. Then start changes to (0,0), end changes to (end-start), bounding rect is from (0,0) to new value for end adjusted to fit the arrow head. Finally you need to use QGraphicsItem::setPos() to move the arrow to the original value of start.

    I don't want to analyze your current calculations, if I were to guess what was wrong with it is that you didn't adjust for the arrow head thus painting outside the bounding rectangle of the item. So technically speaking you should fix only that but please also consider what I have written in the beginning.
    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.


  3. #3
    Join Date
    Jul 2012
    Location
    Switzerland
    Posts
    32
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with derived class from QGraphicsItem and drawing

    yes, your right, I'm calling changleLine with scene coordinates. Is it not better to try that my left upper corner is always (0/0)? Because start isn't always my left upper corner

    I've tried to find my mistake with QRectF but haven't found one that solves my problem

    The points of the triangle (pointOne, pointTwo) are correct I've checked that and the QRectF too

    Qt Code:
    1. #include "arrowitem.h"
    2.  
    3. ArrowItem::ArrowItem(QGraphicsItem *parent)
    4. :QGraphicsItem(parent)
    5. {
    6. }
    7.  
    8. void ArrowItem::changeLine(QPointF start, QPointF end)
    9. {
    10. double penwidth = 3.0;
    11.  
    12. double radius = 5.0;
    13. //Point which lies on line which indicates begining of the Arrow
    14. // ---|>
    15. QPointF onLine = 19.0/20.0*(end-start)+start;
    16. QPointF pointOne;
    17. QPointF pointTwo;
    18.  
    19. double width;
    20. double height;
    21.  
    22. double top;
    23. double left;
    24.  
    25. if((start.x() != end.x()) && (start.y() != end.y()))
    26. {
    27. double m = (end.y()-start.y())/(end.x()-start.x());
    28. double mRect = -1/m;
    29. double b = onLine.y()- mRect*onLine.x();
    30. double c = qPow(mRect,2)+1.0;
    31. double d = 2.0*b*mRect -2.0*onLine.x() -2.0*mRect*onLine.y();
    32. double e = qPow(b,2) + qPow(onLine.x(),2) + qPow(onLine.y(),2) -2.0*b*onLine.y() -qPow(radius,2);
    33.  
    34. double xOne = (-d + qSqrt(d*d-4.0*c*e))/(2.0*c);
    35. double yOne = xOne*mRect + b;
    36. double xTwo = (-d - qSqrt(d*d-4.0*c*e))/(2.0*c);
    37. double yTwo = xTwo*mRect + b;
    38.  
    39. pointOne = QPointF(xOne,yOne);
    40. pointTwo = QPointF(xTwo,yTwo);
    41.  
    42. double distanceX;
    43. double distanceY;
    44.  
    45. //get width & left
    46. if(start.x() < end.x())
    47. {
    48. if(pointOne.x() > pointTwo.x())
    49. {
    50. distanceX = pointOne.x();
    51. }else{
    52. distanceX = pointTwo.x();
    53. }
    54. if(end.x() > distanceX)
    55. {
    56. distanceX = end.x();
    57. }
    58. width = distanceX - start.x();
    59. left = start.x();
    60. }else{
    61. if(pointOne.x() < pointTwo.x())
    62. {
    63. distanceX = pointOne.x();
    64. }else{
    65. distanceX = pointTwo.x();
    66. }
    67. if(end.x() < distanceX)
    68. {
    69. distanceX = end.x();
    70. }
    71. width = start.x() - distanceX;
    72. left = distanceX;
    73. }
    74.  
    75. //get height & top
    76. if(start.y() > end.y())
    77. {
    78. if(pointOne.y() < pointTwo.y())
    79. {
    80. distanceY = pointOne.y();
    81. }else{
    82. distanceY = pointTwo.y();
    83. }
    84.  
    85. if(end.y() < distanceY)
    86. {
    87. distanceY = end.y();
    88. }
    89.  
    90. height = start.y() - distanceY;
    91. top = distanceY;
    92. }else{
    93. if(pointOne.y() > pointTwo.y())
    94. {
    95. distanceY = pointOne.y();
    96. }else{
    97. distanceY = pointTwo.y();
    98. }
    99.  
    100. if(end.y() > distanceY)
    101. {
    102. distanceY = end.y();
    103. }
    104. height = distanceY - start.y();
    105. top = start.y();
    106. }
    107.  
    108. }else{
    109. if(start.x() == end.x())
    110. {
    111. pointOne = QPointF(onLine.x() + radius, onLine.y());
    112. pointTwo = QPointF(onLine.x() - radius, onLine.y());
    113.  
    114. width = 2.0*radius;
    115. left = pointTwo.x();
    116.  
    117. if(start.y() > end.y())
    118. {
    119. height = start.y()-end.y();
    120. top = end.y();
    121. }else{
    122. height= end.y()-start.y();
    123. top = start.y();
    124. }
    125.  
    126.  
    127. }else{
    128. pointOne = QPointF(onLine.x(), onLine.y() + radius);
    129. pointTwo = QPointF(onLine.x(), onLine.y() - radius);
    130.  
    131. height = 2.0*radius;
    132. top = pointTwo.y();
    133.  
    134. if(start.x() < end.x())
    135. {
    136. left = start.x();
    137. width = end.x()-start.x();
    138. }else{
    139. left = end.x();
    140. width = start.x()-end.x();
    141. }
    142. }
    143. }
    144.  
    145. triangle << pointOne << pointTwo << end << pointOne;
    146. line.setPoints(start, end);
    147.  
    148. prepareGeometryChange();
    149. rect = QRectF(left -penwidth/2,top -penwidth/2,width + penwidth,height + penwidth);
    150.  
    151. update();
    152. }
    153.  
    154. QRectF ArrowItem::boundingRect() const
    155. {
    156. return rect;
    157. }
    158.  
    159. void ArrowItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    160. {
    161. painter->setPen(QPen(Qt::black, 3, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
    162. painter->drawLine(line);
    163.  
    164. //painter->setBrush(QBrush(Qt::black));
    165. painter->drawPolygon(triangle);
    166. }
    To copy to clipboard, switch view to plain text mode 
    That shouldn't make a difference but I'm using the Item as a child of a bigger item.

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

    Default Re: Problem with derived class from QGraphicsItem and drawing

    Quote Originally Posted by airglide View Post
    yes, your right, I'm calling changleLine with scene coordinates. Is it not better to try that my left upper corner is always (0/0)? Because start isn't always my left upper corner
    I didn't say anything about start being your top-left corner. I said it is the origin (the point where your item is "attached" to its parent item). The item can have negative coordinates, e.g. it's quite common that the origin is in the centre of the item.

    I've tried to find my mistake with QRectF but haven't found one that solves my problem

    The points of the triangle (pointOne, pointTwo) are correct I've checked that and the QRectF too
    Your boundingRect calculation is incorrect, that's for sure. I changed your paint() routine to draw the boundingRect() of the item and if I create a line that is almost horizontal (e.g. from 30, 30 to 300, 32) the arrow head extends past the bounding rect.
    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.


  5. The following user says thank you to wysota for this useful post:

    airglide (18th February 2013)

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with derived class from QGraphicsItem and drawing

    You know, you could probably eliminate almost all of these complex calculations if you simply made a "standard" arrow of unit size (e.g. length 1.0 and arrowhead size as whatever fraction of that makes a pleasing triangle) pointing from left to right on the X axis. This would be done once in the item constructor.

    Then in your change arrow method, use the start and end points to calculate the angle of the line with respect to the x axis and set the rotation transformation accordingly. If the whole arrowhead should scale for larger distances between start and end, then set the scale transformation too.

    You basically have almost no geometry calculation at all: an arccosine to determine the angle, and a couple of ratios to determine scale factors. Your bounding rect is simply determined by rotating the upper left and lower right points by the angle of the arrow.

  7. The following user says thank you to d_stranz for this useful post:

    airglide (18th February 2013)

  8. #6
    Join Date
    Jul 2012
    Location
    Switzerland
    Posts
    32
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with derived class from QGraphicsItem and drawing

    thanks for clearafing the first one, you were right with the second one, I've fixed that but the real problem wasn't that, I made a silly mistake,
    Qt Code:
    1. triangle << pointOne << pointTwo << end << pointOne;
    To copy to clipboard, switch view to plain text mode 
    it added the new points to the old one's and because of that i had all this triangles which were correctly repainted

    thank you for your help )

    @d_stanz
    yes, you're right. It was only a prototype but it made me crazy because it didn't work. I'll consider this when i'll design the real class, thanks
    Last edited by airglide; 18th February 2013 at 17:39.

  9. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with derived class from QGraphicsItem and drawing

    In fact, you don't have to implement any of the transformations I described in the arrow class itself. All the arrow class has to do is to draw a "unit arrow" pointing from left to right.

    The scene (or whatever graphics item contains the arrow as a child) is responsible for transforming it to the right size, pointing in the right direction. You create the arrow shaft and head in the constructor with unit dimensions, and paint them that way in the paint() method. The QPainter instance that comes into the paint() method has already been transformed to draw the arrow the way it is supposed to appear on screen.

    So your arrow class simplifies to just the paint() method, and the bounding rect is simply the constant bounds of the unit arrow. You don't have to implement any behavior that responds to changes in size or direction - that's the responsibility of the scene or parent item.

    All you have to decide is where the origin of your arrow will be. Where is (0,0) on the unit arrow? At the start of the line, tip of the arrowhead, or somewhere in between? This might depend on how you anticipate using the arrow - will it be used to point at something, or away from something? If it points at something, then you probably want the origin to be at the tip, otherwise it should be at the start of the line. Whichever you decide, it would be nice to add some convenience functions to return the QPoints corresponding to each end of the arrow so if you use it to connect two things, you can easily get both ends and convert them to screen positions.

Similar Threads

  1. QwtPlot derived class
    By Marco Trapanese in forum Qwt
    Replies: 15
    Last Post: 4th December 2012, 10:05
  2. Replies: 0
    Last Post: 7th July 2012, 11:32
  3. Replies: 3
    Last Post: 6th April 2012, 16:44
  4. Replies: 2
    Last Post: 12th May 2010, 13:32
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

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.