Results 1 to 12 of 12

Thread: how to use mouse move event to drag curves on QGraphicsScene?

  1. #1
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. drawGlyphs=new QPixmap(800, 800);
    2. drawGlyphs->fill(Qt::white);
    3. QPainter p(drawGlyphs);
    4. p.setViewport(50,200,400,400);
    5. p.setPen(Qt::black);
    6. p.drawPath(mypath);
    7. pScene->addPixmap(*drawGlyphs);
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. class mySubClass1 : public QGraphicsPathItem
    2. {
    3. public:
    4. mySubClass1();
    5. protected:
    6. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    7. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to use mouse move event to drag curves on QGraphicsScene?

    can you show the content of your mosuePressEvent() override?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to use mouse move event to drag curves on QGraphicsScene?

    My implementation of mousePressEvent is:

    Qt Code:
    1. void mySubClass1::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if(event->button() == Qt::LeftButton)
    4. {
    5. qDebug()<<"in musubclass mouse press event: "<<event->pos().x()<<" "<<event->pos().y();
    6. if(shape().contains(event->pos()))
    7. {
    8. currPosX=event->pos().x();
    9. currPosY=event->pos().y();
    10. qDebug()<<"currPosX currPosY: "<<currPosX<<" "<<currPosY;
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to use mouse move event to drag curves on QGraphicsScene?

    And? do you get the debug message?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.
    Attached Files Attached Files

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to use mouse move event to drag curves on QGraphicsScene?

    Well sorry but did not get you...
    What exactly didn't you get? how can I make my question any simpler?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?
    Last edited by wysota; 19th September 2013 at 10:39.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to use mouse move event to drag curves on QGraphicsScene?

    Quote Originally Posted by sarbh20ss View Post
    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.

    I was thinking of implementing mouse move event of QGraphicsPathItem.
    Great, and what would you put in that event?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jun 2007
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use mouse move event to drag curves on QGraphicsScene?

    Quote Originally Posted by wysota View Post
    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:
    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class Path;
    4.  
    5. class Node: public QGraphicsEllipseItem {
    6. public:
    7. Node(Path* path, int index);
    8. protected:
    9. virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    10. private:
    11. static const qreal rad;
    12. Path* path;
    13. int index;
    14. };
    15.  
    16. const qreal Node::rad = 5;
    17.  
    18. class Path: public QGraphicsPathItem {
    19. public:
    20. Path(const QPainterPath& path, QGraphicsScene* scene);
    21.  
    22. void updateElement(int index, const QPointF& pos);
    23. private:
    24. };
    25.  
    26. Node::Node(Path* path, int index)
    27. : QGraphicsEllipseItem(-rad, -rad, 2*rad, 2*rad),
    28. path(path) , index(index)
    29. {
    30. setZValue(1);
    31. setFlag(QGraphicsItem::ItemIsMovable);
    32. setFlag(QGraphicsItem::ItemSendsGeometryChanges);
    33. setBrush(Qt::green);
    34. }
    35.  
    36. QVariant Node::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
    37. {
    38. if (change == ItemPositionChange) {
    39. path->updateElement(index, value.toPointF());
    40. }
    41. return QGraphicsEllipseItem::itemChange(change, value);
    42. }
    43.  
    44. Path::Path(const QPainterPath& path, QGraphicsScene* scene)
    45. : QGraphicsPathItem(path), path(path)
    46. {
    47. for (int i = 0; i < path.elementCount(); ++i) {
    48. auto node = new Node(this, i);
    49. node->setPos(path.elementAt(i));
    50. scene->addItem(node);
    51. }
    52. setPen(QPen(Qt::red, 1.75));
    53. }
    54.  
    55. void Path::updateElement(int index, const QPointF &pos)
    56. {
    57. path.setElementPositionAt(index, pos.x(), pos.y());
    58. setPath(path);
    59. }
    60.  
    61. int main(int argc, char *argv[])
    62. {
    63. QApplication app(argc, argv);
    64. path.moveTo(0, 0);
    65. path.cubicTo(-30, 70, 35, 115, 100, 100);
    66. path.lineTo(200, 100);
    67. path.cubicTo(200, 30, 150, -35, 60, -30);
    68. auto scene = new QGraphicsScene();
    69. scene->addItem(new Path(path, scene));
    70. auto view = new QGraphicsView(scene);
    71. view->setRenderHint(QPainter::Antialiasing);
    72. view->resize(600, 400);
    73. view->show();
    74. return app.exec();
    75. }
    To copy to clipboard, switch view to plain text mode 
    ?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Problem with QGraphicsScene mouse move event
    By Nesbit in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2012, 18:12
  2. Replies: 3
    Last Post: 7th January 2012, 08:38
  3. QGraphicsScene drag event
    By rbp in forum Qt Programming
    Replies: 10
    Last Post: 13th May 2009, 14:03
  4. mouse move event filtering in PyQt4
    By lucaf in forum Qt Programming
    Replies: 1
    Last Post: 4th April 2009, 14:53
  5. Mouse Move Event
    By merry in forum Newbie
    Replies: 5
    Last Post: 3rd June 2007, 06:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.