Results 1 to 9 of 9

Thread: Drawing background while mouseMoving

  1. #1
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Drawing background while mouseMoving

    Hello,

    I have an interesting problem I am trying to solve. I have a derived class of QGraphicsScene that draws a line in a rubber band fashion while the left mous button is depressed. What this means is that while I have the left button down it will show the line and I can draw it's position and length however I wish until I release the mouse button. At that time it will draw the object where it was when I left the button up.

    What I want to do WHILE I am in rubberband mode, is let appear a cartesian plane axis whose origin is at the point I clicked the left mouse button to begin with. The cartesian plane axis' will continue to appear until I let the mouse button up. In addition, I wish to draw an arc between the x-axis and the line and display it's angle.

    Please see attached picture for a view of what I want to see if I have the mouse button clicked and held down.

    The problem I am having is that the cartesian axis seem to appear randomly and do not persist while I have the button clicked. I am defining it in the QGraphicsScene::drawbackground() overloaded function instead of the void QGraphicsScene::mouseMoveEvent(QGraphicsSceneMouse Event *mouseEvent) because of the access to the QPainter object.

    Is there a better place to do this?

    Cheers!
    -Caolan

    20160701_112930[1].jpg

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing background while mouseMoving

    Hmm.

    What do you mean with "it appears randomly"?

    Do you call update() in the methods that affect if/how the background is drawn?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Drawing background while mouseMoving

    As I hold the mouse button down and move the vector around it's p1 anchor point, the grey image I am trying to draw sometimes shows up and when it dies, it is a partial draw.

    No i am not calling update().

    Qt Code:
    1. void PhysGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect) {
    2. Q_UNUSED(rect);
    3.  
    4. QMatrix mtx;
    5. int w_2 = width() / 2;
    6. int h_2 = height() / 2;
    7.  
    8. painter -> setPen(Qt::blue);
    9. painter -> drawLine(0, h_2, width(), h_2); // X-Axis
    10. painter -> drawLine(w_2, 0 , w_2, height()); // Y-Axis
    11.  
    12. mtx.translate(w_2, h_2);
    13. mtx.scale(1, -1);
    14.  
    15. painter -> setMatrix(mtx);
    16. painter -> setPen(Qt::NoPen);
    17. painter -> setBrush(QBrush(Qt::blue, Qt::Dense4Pattern));
    18. painter -> drawRect(-10, -10, 20, 20);
    19.  
    20. QLineF y_axis(-10, -10, -10, 10);
    21. QLineF x_axis(-10, -10, 10, -10);
    22. QPen pen;
    23. QLineF angleLine1, angleLine2;
    24.  
    25. pen.setWidth(2);
    26. pen.setColor(Qt::red);
    27. painter -> setPen(pen);
    28.  
    29. setupAngleLine(angleLine1, x_axis.p2(), x_axis.p1(), 135.0);
    30. setupAngleLine(angleLine2, x_axis.p2(), x_axis.p1(), -135.0);
    31. painter -> drawLine(x_axis); // X-Axis
    32. painter -> drawLine(angleLine1);
    33. painter -> drawLine(angleLine2);
    34.  
    35. pen.setColor(Qt::green);
    36. setupAngleLine(angleLine1, y_axis.p2(), y_axis.p1(), 45.0);
    37. setupAngleLine(angleLine2, y_axis.p2(), y_axis.p1(), 135.0);
    38. painter -> setPen(pen);
    39. painter -> drawLine(y_axis); // Y-Axis
    40. painter -> drawLine(angleLine1);
    41. painter -> drawLine(angleLine2);
    42.  
    43. if (m_Mode == InsertLine && m_pLine)
    44. drawAngledPlane(painter);
    45. }
    46.  
    47. void PhysGraphicsScene::drawAngledPlane(QPainter *painter) {
    48. QPointF lineMidPoint = findLineMidPoint(m_pLine ->line());
    49. QRectF lineRect(m_pLine ->line().p1(), m_pLine ->line().p2());
    50.  
    51. // Draw the temporary cartesian axis' origin'ed at p1 of the line
    52. QPen pen(Qt::lightGray);
    53. pen.setStyle(Qt::DashDotDotLine);
    54. pen.setWidth(2);
    55. painter ->setPen(pen);
    56. QLineF xAxis, yAxis;
    57. xAxis.setAngle(90);
    58. xAxis.setP1(m_pLine ->line().p1()); xAxis.setP2(QPointF(m_pLine ->line().dx(), 0));
    59. xAxis.setLength(m_pLine ->line().dx());
    60.  
    61. yAxis.setAngle(0);
    62. yAxis.setLength(m_pLine ->line().dy());
    63.  
    64. painter -> drawLine(xAxis); // x-axis
    65. painter -> drawLine(yAxis); // y-axis
    66.  
    67.  
    68. // Draw the arc from the false cartesian axis to the line
    69. double angle = m_pLine ->line().angle();
    70. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing background while mouseMoving

    You need to call update() when the thing you want to draw changes.

    As for the partial draw: maybe the coordinates are off? You are not using the rectangle that is passed to drawBackground. It is the part of the background that is currently exposed.

    Alternatively you could create a custom graphics item or an combination of items for this and just locate it below the actual item.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Drawing background while mouseMoving

    I implemented the update and one of the liens I draw do persist but it does not stay in position and moves about while I move the vector. I'll check out the solutions you present. BTW, I am basing this off of one of the samples used to show how to use QGraphicsScenes and it does not utilize a QGraphicsView except in the most offhanded way. Other samples I have seen do the opposite and fully flesh out the use of QGraphicsView but treats the QGraphicsScene as if it was not important. However the documentation i have been reading seems to think they're both equally important. Is there a sample Drawing app that shows how both are used as derived classes, in conjunction with QT Creator, to build out a rich, custom interface?

    I ask because the application I have been trying to develop seems to need both and I want to make sure I don't spend more time going down yet another wrong road because I am ignorant of what a good QT drawing application looks like.

    Cheers!
    -Caolan.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing background while mouseMoving

    One usually only needs to derive from the scene for things like rendering a custom background, otherwise it is just populated with items.
    Very similar for the view, which usually only needs to be subclassed if one needs custom event handling on the view level.

    Cheers,
    _

  7. #7
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Drawing background while mouseMoving

    Okay so should the painting of an object such as a QGraphicsLineItem, happen within the subclass of that item in a custom paint?

    I am currently implementing a custom QGraphicsView, have a default QGraphicsScene, and each of the objects implement their own paint() but I am encountering some serious issues with drawing which is why I was looking at having it done in a custom QGraphicsScene instead because the sample I was mocking it up in seems to have made rendering the objects easier.

    Should I stick with my initial implementation of having a custom QGraphicsView and where each QGraphicsItem does it's own custom paint()?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing background while mouseMoving

    Each item paints itself, even with the standard view.

    The view asks the scene to render a specific area, the scene finds the items intersecting this area and asks them to paint themselves.

    Cheers,
    _

  9. #9
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Drawing background while mouseMoving

    Ooooooh I see now... Okay that makes sense. SO if I wanted to ensure that the entire screen is always rendered, I need to make sure the scene's rectangle is always set to the current size of the View as well....

    Thank you!
    -Caolan

Similar Threads

  1. Replies: 2
    Last Post: 28th April 2012, 08:58
  2. Replies: 0
    Last Post: 8th April 2010, 16:06
  3. Replies: 4
    Last Post: 15th March 2010, 14:49
  4. Drawing background of a scene
    By maverick_pol in forum Qt Programming
    Replies: 0
    Last Post: 11th January 2008, 11:12
  5. Drawing the background of QGraphicsView
    By aknuds1 in forum Qt Programming
    Replies: 13
    Last Post: 9th March 2007, 14:53

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.