Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to draw a movable line on Widgets
Is a possible to draw a movable line say on QGraphicsView. Something line displacing the line parallel to itself using mouse
Of course you can, just get a QLine member variable, have the method to move it, either by animation or mouse or whatever and draw that line in your paint event.
mherrero last edited by
"ddriver":http://qt-project.org/member/9887. Can You indicate some script of example in PySide?
No, sorry, I don't know Python all that well and I haven't used PySide, I am a c++ guy. Just look at the documentation, it is a single QPainter method to draw a line, and some generic programming logic to set up your line's coordinates.
But this does not draw on QGraphicsView widget, the images displayed in QgraphicsView hide the line, i want it to display line on top of every widget
@void aViewer::paintEvent(QPaintEvent *)
QPen pen(Qt::black, 2, Qt::SolidLine);
Why don't you create the line as a QGraphicsItem and set it to be on the very top in the view, so that all other items are underneath?
As far as moving the line is concerned, its behaving wierdly, i don't have much experience in event handling. This is what I tried:
@void aViewer::mousePressEvent(QMouseEvent *event)
origin = event->pos();
void aViewer::mouseMoveEvent(QMouseEvent *event)
void aViewer::mouseReleaseEvent(QMouseEvent *event)
// not coded yet
I want to implement a sort of drag and drop on the line, that is I drag the line and it moves with the drag and gets repositioned parallel to itself.
The above code is just causing random parallel displacements making no sense.
A somewhat detailed help will be highly appreciated.
Actually I am implementing a crop function. I want to draw a rectangle (perhaps using four lines) on an image displayed in qgraphicsview widget. the rectangle should be resizable on its ends using mouse. Once the user finalises the part of image, the program would use copy function of QImage to extract the cropped part.
Why don't you try something a bit different - instead of lines create two QPoints, two points is all you need to define a rectangular area. Set the first point on mouse press, on mouse move set the second point, and in the paint event, if both points are set, draw a translucent rectangle with those two points to get visual feedback of the crop area. If you need to adjust the crop area afterwards you will have to create handles to control those two points
andre last edited by
If you are using the QGraphicsView framework anyway, I would just use a QGraphicLineItem that you make movable by setting the ItemIsMovable flag on it.
jmoellmer last edited by
I think one of the key things to understand is how high-level events work. Take a look at QGraphicsItem::itemChange(). If you are using QGraphicsView framework and your line items are QGraphicLineItem, then you can set the flags to selectable and movable, over-ride itemChange(), and then you can make all sorts of magic happen!
Hope this helps a little.