Qt World Summit: Submit your Presentation

How to draw interactive geometrical shapes, that will allow me to drag their corners?

  • Happy New Year fellows!

    I want to create a calculator like this, so the user can enter some values for 2 lines and their angle for a triangle and the program will calculate and report back other info.

    It's not that difficult to do this with a QGraphicsScene pbject but I also want to drag the edges so that I create my own triangle. After that the spinboxes will be updated with the new data and points and new calculations will be triggered.

    So my questions are the following:

    1. Can I drag a shape/line/edge/any geometrical entity on a QGraphicsScene or anywhere else?

    2. Can I somehow create a grid that will understand the concept of points and give me the ability to calculate the length of that line, or calculate the area of a triangle?

    Is there an API for this or do I have to do the complex mathematics myself?

  • @Petross404_Petros-S To drag the corner points of a triangle, I would suggest that you find the closest corner point to the mouse point. Move the mouse point to that location and let the user drag that point. Recall that distance can be found by qSqrt(QPointF::dotProduct(p1, p2)).

  • @ofmrew I forgot to mention that if you are using QPainterpath objects, then you will need to the the elements that form the path. You can get the points you need from the elements.

  • Lifetime Qt Champion


    1. Yes you can but you have to handle the mouse event yourself.
    2. Isn't that the role of your shape ?

    You can pick the basics on how to do it from this stackoverflow question. Even though it's in python you can pretty easily translate it back to C++.

  • Thank you both for those hints!

Log in to reply