how to use mouse move event for QGraphicsScene?
Asked Answered
P

2

2

hey want to drag this bezier curve when mouse button is pressed and moved..

I did this:

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
qDebug()<<"in mouse move - outside if";
if((e->buttons() & Qt::RightButton) && isStart && enableDrag)
{
    qDebug()<<"mouse dragging happening";
    xc2=e->pos().x();
    yc2=e->pos().y();
    drawDragBezier(xc2,yc2);
}
}

this starts dragging when i press right button and start moving mouse in whole main window..but I want to start dragging only when I press mouse button and move mouse inside the QGraphicsScene.

how to solve this?

EDIT:

void mySubClass1::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
    qDebug()<<"in musubclass mouse press event: "<<event->pos().x()<<" "
<<event- >pos().y();
    if(shape().contains(event->pos()))
    {
        currPosX=event->pos().x();
        currPosY=event->pos().y();
        qDebug()<<"currPosX currPosY: "<<currPosX<<" "<<currPosY;
    }
}
}

And the mainwindow class is:

{
myGPath=new mySubClass1();
myScene=new QGraphicsScene;
myScene->addItem(myGPath);
ui->graphicsView->setScene(myScene);


QPointF *startPoint=new QPointF(50,50);
myPaintPath=new QPainterPath(*startPoint);

myPaintPath->quadTo(100,25,200,200);

myGPath->setPath(*myPaintPath);
}

is this the right way?

Presently answered 15/9, 2013 at 21:11 Comment(0)
A
7

Personally, to solve this issue I'd take a different approach.

Create a class inherited from QGraphicsItem (or QGraphicsObject if you want signals and slots) to represent the bezier curve. Then implement the mouseMoveEvent of the object in this class.

class MyBezierCurve : public QGraphicsItem
{
    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent*);
        void mouseMoveEvent(QGraphicsSceneMouseEvent*);
        void mouseReleaseEvent(QGraphicsSceneMouseEvent*);

};

This way, the object can detect in its mousePressEvent when the mouse is directly over one of its control points and update the control points with mouse move events until the release event occurs.

Handling the mouse events in the QGraphicsView will work, but if you introduce more bezier curves, or other objects you'll find you'll need to check which of them you need to be interacting with. Handling it in the object itself will take care of that for you.

Aftermost answered 16/9, 2013 at 7:54 Comment(4)
So i have to subclass the QGraphicsPathItem..or how do I draw or add path in object of subclass of QGraphicsItem ?Presently
You can either subclass QGraphicsPathItem, or you can create a class that has a QGraphicsPathItem as a private member. The first option would be my preferred method. Once subclassed, you can use the shape() function, which returns a QPainterPath which will then provide the contains() function to test if the mouse is over the object.Aftermost
I tried as u suggested. Say there are 3 object of this subclass in which I am implementing mouse events. But now how to check which of the three objects caused mouse event. I am doing this: 1.subclassed QGraphicsPathItem 2.Created object of this class and painter path object 3.set path for this object 4.add this object to graphics view is this the right way...need guidance??Presently
That's the point, you don't need to check; because you're implementing the mouse events in the class, you'll only receive the mouse events for the relevant class instance. Qt will handle that for you. If you're still unsure please add the code you've tried, as an edit to your question.Aftermost
F
3

You should subclass QGraphicsView and detect mouseMoveEvent over there.

class MyGraphicsView : public QGraphicsView
{
   Q_OBJECT
   ...
protected:       
   void mouseMoveEvent(QMouseEvent *event);  
   ... 
};
Factory answered 16/9, 2013 at 7:2 Comment(4)
I used subclass of QGraphicsView as you suggested..and it worked. Thanks. But still the dragging is not happening as expected. I mean I want curve to move along the mouse pointer when I press right button. I want to be able to drag the curve when mouse pointer is actually on one of it's control points. How to do that?Presently
@user2739283 Are you saying that that the mouse position in your moseMoveEvent is not the mouse position it should be in the scene? If that is the case use QGraphicsView::mapToScene(const QPoint & point).Factory
Hey I am sorry may be I asked question in wrong way. I want to be able to drag my curve the way we can drag it in CAD applications.Presently
No. Actually I want to display the control point of my bezier curve. When user clicks on that point and drags the mouse I want to redraw the curve till mouse button is released. So how to do that?Presently

© 2022 - 2024 — McMap. All rights reserved.