how to use mouse move event to drag curves on QGraphicsScene?
hey I am drawing a bezier curve on QGraphicsScene.
Initialy I was drawing curve using path and drawing it in pixmap and then adding pixmap to scene.
This allows me to add multiple paths to scene through pixmap.
Code:
drawGlyphs->fill(Qt::white);
p.setViewport(50,200,400,400);
p.setPen(Qt::black);
p.drawPath(mypath);
pScene->addPixmap(*drawGlyphs);
I wanted to check if the mouse click is on one of the paths. But with above structure I couldn't do that.
So I subclass QGraphicsPathItem.
Code:
{
public:
mySubClass1();
protected:
}
Here I am drawing curve in painter path, setting this path to object of 'mySubClass1' and adding this object as item to scene.
Now when I try to implement mouse move event I could not detect on which point which is on the curve I clicked and trying to drag.
How do I solve this problem?
Is the structure of my program correct?
Re: how to use mouse move event to drag curves on QGraphicsScene?
can you show the content of your mosuePressEvent() override?
Re: how to use mouse move event to drag curves on QGraphicsScene?
My implementation of mousePressEvent is:
Code:
{
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;
}
}
}
Re: how to use mouse move event to drag curves on QGraphicsScene?
And? do you get the debug message?
3 Attachment(s)
Re: how to use mouse move event to drag curves on QGraphicsScene?
Well sorry but did not get you...
I have attached my complete program. I actually want to detect the mouse click at particular point and mouse move for same point.
Hope you will help solve me this problem.
Re: how to use mouse move event to drag curves on QGraphicsScene?
Quote:
Well sorry but did not get you...
What exactly didn't you get? how can I make my question any simpler?
Re: how to use mouse move event to drag curves on QGraphicsScene?
I am drawing a path on scene using an object of subclass of QGraphicsPathItem. At particular points on this path I want to detect the mouse click and mouse move event.
According to current coordinates of mouse pointer given by mouse move event I want to redraw this path.
In a program that I have attached in previous post I am able to detect the press event but want to check if click is on those particular points. If yes then using move event I want to redraw it. But I am not able to understand how to pass these new coordinates to a function which generates this path.
So How to do that?
Re: how to use mouse move event to drag curves on QGraphicsScene?
In my opinion your approach makes completely no sense. You should have items for nodes and separate items for curves (or lines) between nodes and manipulate curves using those node items. But you can't implement it in 10 lines of code no matter how much you try it. Trying to detect control points for curves by observing coordinates is a totally bogus approach in an object oriented system such as Graphics View.
Don't take it personal but it's more and more often situation that I'm surprised people start programming without thinking first how they are going to implement the whole solution (in terms of designing the architecture, not writing the actual code) and they end up trying to implement some wacky solutions instead of using standard approaches used by everyone else in the industry. For instance -- how did you plan to move the control point in the path to some other position?
Re: how to use mouse move event to drag curves on QGraphicsScene?
Well I will not take it personally.
Can you advice me where I can study or get to know the 'standard approaches in industry' for Graphics related coding or how to design architecture for such type of Graphics coding.
I was thinking of implementing mouse move event of QGraphicsPathItem.
Appreciate your advice on showing me the right way.
Re: how to use mouse move event to drag curves on QGraphicsScene?
Quote:
Originally Posted by
sarbh20ss
Can you advice me where I can study or get to know the 'standard approaches in industry' for Graphics related coding or how to design architecture for such type of Graphics coding.
Look at standard programs -- Photoshop, Gimp, Corel Draw, Autocad, etc. etc. Think how they might be implementing the functionality they offer.
Quote:
I was thinking of implementing mouse move event of QGraphicsPathItem.
Great, and what would you put in that event?
Quote:
Appreciate your advice on showing me the right way.
I already told you in the previous post what in my opinion is the right way.
Re: how to use mouse move event to drag curves on QGraphicsScene?
Quote:
Originally Posted by
wysota
You should have items for nodes and separate items for curves (or lines) between nodes and manipulate curves using those node items.
Do you mean that something like this can be used:
Code:
#include <QtWidgets>
class Path;
public:
Node(Path* path, int index);
protected:
private:
static const qreal rad;
Path* path;
int index;
};
const qreal Node::rad = 5;
public:
void updateElement(int index, const QPointF& pos);
private:
};
Node::Node(Path* path, int index)
path(path) , index(index)
{
setZValue(1);
setBrush(Qt::green);
}
{
if (change == ItemPositionChange) {
path->updateElement(index, value.toPointF());
}
}
{
for (int i = 0; i < path.elementCount(); ++i) {
auto node = new Node(this, i);
node->setPos(path.elementAt(i));
scene->addItem(node);
}
setPen
(QPen(Qt
::red,
1.75));
}
void Path
::updateElement(int index,
const QPointF &pos
) {
path.setElementPositionAt(index, pos.x(), pos.y());
setPath(path);
}
int main(int argc, char *argv[])
{
path.moveTo(0, 0);
path.cubicTo(-30, 70, 35, 115, 100, 100);
path.lineTo(200, 100);
path.cubicTo(200, 30, 150, -35, 60, -30);
scene->addItem(new Path(path, scene));
view
->setRenderHint
(QPainter::Antialiasing);
view->resize(600, 400);
view->show();
return app.exec();
}
?
Re: how to use mouse move event to drag curves on QGraphicsScene?
I don't have a ready answer for every problem. What you suggested seems reasonable but I can't say whether it is a good approach or not. You need to think whether it satisfies your requirements or not.