PDA

View Full Version : any idea for doing this project by Qt



QJak
5th March 2019, 16:37
hi, there are 5 points. some of these points have connection with anothers. for example point A is connected to Point E and point E is connected to point D. point B is connected to point C. I attached the picthere of these connections below. is under two things posiable by Qt?
1- drawing points in different color like the uploaded picture?
2- conncting these points together by a line easily by clicking on point?

thanks

d_stranz
5th March 2019, 18:45
Of course, anything is possible with Qt :D

I would use the Qt Graphics / View architecture for this. QGraphicsEllipseItem can be used for the circles, QGraphicsSimpleTextItem or QGraphicsTextItem for the labels, and QGraphicsPathItem and a QPainterPath for the lines.

If you want to be able to move the points around and have the endpoints of the lines follow the points, you'll need to add code for that. A good example is the Elastic Nodes demo project in the Qt distro. The Drag and Drop Robot example and the Diagram Scene example show how to interact with graphics items in a scene through a graphics view.

Cruz
9th March 2019, 09:28
Alternatively, you could also familiar yourself with the QPainter object and use it's drawEllipse() and drawLine() methods to implement the drawing of the dots and the lines in the paintEvent() of a QWidget, and use the mouseMoveEvent() and mousePressedEvent() handlers to implement your own logic. The QGraphicsView environment does make things easier in the long run as it automatically takes care of things for you like routing mouse clicks happening on the dots into certain functions, but it also requires a steep learning curve on top of understanding how QWidgets, events, and painting work which might scare off a Qt beginner, as I believe the OP is. Using a raw QWidget + QPainter environment is much easier to get you started, imho.

d_stranz
9th March 2019, 18:21
I agree in part, if all you need are static images on screen. But I personally think it is much harder to get interaction with graphics on screen to work when you have to implement all that code yourself. Being able to create a QGraphicsItem with properties that allow it to be movable, and then just being able to click on it and move it (with all of that logic being handled by the G/V framework) is much easier than having to invent that wheel yourself. Likewise, being able to derive a complex QGraphicsItem and implement the drawing in its own coordinate system, knowing that the G/V framework can do things like make copies, scale, rotate, and translate it without any changes to your code is a big plus. And being able to create a QGraphicsScene once and display different viewports on it in different windows with no code changes to the scene is fantastic.

I've done complex scientific graphics both ways. For me, the G/V framework will be the way to go for future needs.