Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35

Thread: Rotating a QGraphicsLineItem in a QGraphicsScene

  1. #21
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    @stampede - How can we translate a parent line's point P1 which currently is mouse move event's pos() back to its local coordinate system?
    In the below code, if anchor is P2, then P1 takes mouse event's pos() as its new coordinates. Originally P1 has (0,0) as its coordinates, since we are in item coordinate system. Once the resize or rotate is complete, the P1 coordinates are set to event->pos() coordinates.
    Once the rotation/resize of a line is finished, how do I position the item in local coord system again?
    I tried using this->setTransformOriginPoint(event->pos()) but it does not change the item P1 coordinates to origin(0,0).

    This has caused issue while I place the Arrow item on the parent's line mid point and start resizing/rotating the parent using P2 as an anchor.

    void CustomGraphicsLineItem::mouseMoveEvent(QGraphicsSc eneMouseEvent *event)
    {
    qDebug()<<"Line mouse move event";

    if( dragIndex != -1 )
    {
    const QPointF anchor = dragIndex == 0 ? this->line().p1() : this->line().p2();
    this->setLine(dragIndex == 0 ? QLineF(anchor, event->pos()) : QLineF(event->pos(),anchor));
    }
    if(dragIndex == 1)
    {
    this->setTransformOriginPoint(event->pos());
    }
    this->update();
    }

  2. #22
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    This has caused issue while I place the Arrow item on the parent's line mid poin
    So maybe it would be easier just to draw the child line in reimplemented parent's paint(...) method, instead of trying to manage parent-child relation. The arrow is only for display anyway, right ?

  3. #23
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    the Arrow line item needs to handle mouse press events too. If we click on the existing arrow item then it should toggle and display in opposite direction. I am planning to have two independent arrow line item classes each responsible for displaying arrow line in upward and downward direction respectively. Rendering both these arrow items together will display bi-directional scenario too.
    I guess its because of the above reason i cannot draw in the parent's paint() method(or is it still possible?). so is there a way to translate the newly rotated/resized parent line point P1 with event->pos() coordinates to origin(0,0)? how can the translation be achieved?

    I am really very stressed out dealing with all this scenarios and trying to figure out how it can be achieved especially when my graphics understanding is
    bare minimum :-(
    Last edited by jeevan_ravula; 19th March 2014 at 18:48.

  4. #24
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    I guess its because of the above reason i cannot draw in the parent's paint() method(or is it still possible?).
    of course it is
    it can be done with a simple subclass of QGraphicsItem, using basic math on normal vectors to draw both lines and arrows, type of arrow can be simply an enum
    I created an example of such item for you, just to prove it is possible without any explicit "higher math" :) it looks ugly, but it works :)
    sorry the code is without any comments, but I'm already late :/
    have fun
    Attached Files Attached Files

  5. #25
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    @stampede - Thank you very much for your initiative!!! It meant a lot to me. However, in the course of our conversations, I realized that there is quite a bit of lost in translation. I got the impression that the terminology I am using to ask questions is different from what your are understanding. Let me explain clearly what I meant by parent item and child item in my case.

    Parent Item - Its a custom line item class called CustomGraphicsLineItem, derived from QGraphicsLineItem. this line item class is dropped onto the scene from another scene(this scene just holds certain shapes). This line item once dropped onto the scene can be rotated and resized. It is this line item, that acts as a parent to a Arrow item class.

    Child Item - Its again a custom line item class called CustomArrowItem. This class renders the complete arrow, like the sample class which u sent me. The complete arrow here is the line plus triangle shape. The Arrow line item can be rendered in upward or downward direction.

    In the code that i am currently working, the Arrow line item acts a child of the parent line item which is already dropped on the scene. The Arrow line item rests on the parent line item at 90 degrees at any point we intend to position.
    Please check the attached screen shots inorder to get more clear picture.arrow_display_1.jpgarrow_display2.jpg

  6. #26
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    In your discussion with me you were considering parent-child relationship b/w arrow head(triangle) and the straight line on which arrow head is positioned.
    While I kept referring to parent as a graphics line on which the arrow line item, which is child is positioned.

    When an arrow line item is positioned on the parent line, the axis of rotation of both parent and child is the same, hence when we rotate the line item the Arrow line rotates along with parent line.
    In my case arrow line item does not position its starting point the parent line, it cuts/intersects the parent line at 90 degree. In my scenario, it gives the impression of direction flow from one side to other.
    In case such as this, rotating parent item does not rotate the child( arrow line item) along with it perfectly. With each rotation, the arrow line item gets displaced from its initial position. Hope you got the picture?

  7. #27
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    Hope you got the picture?
    I think so
    Ok, so maybe you could give the parent line a pointer to the arrow and let it control the arrow position manually at each rotation, something like:
    Qt Code:
    1. // assuming _arrow is a pointer to Arrow class in Line class
    2. void Line::attach(Arrow * arrow){
    3. if (!_arrow && arrow){
    4. _arrow = arrow;
    5. _arrow->setParentItem(this);
    6. updateArrowItem();
    7. }
    8. }
    9.  
    10. void Line::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ){
    11. if (_dragIndex != -1){
    12. prepareGeometryChange();
    13. if (_dragIndex == 0)
    14. _p1 = event->pos();
    15. else
    16. _p2 = event->pos();
    17. updateArrowItem();
    18. update();
    19. }
    20. }
    21.  
    22. void Line::updateArrowItem(){
    23. if (_arrow) {
    24. // this will attach arrow on the center of line item, but it is easy to change that with different 'center' point
    25. const QPointF center = (_p1+_p2)/2;
    26. QLineF normal = QLineF(_p1,_p2).normalVector();
    27. normal.setLength(20); // or whatever else arrow length you need
    28. const QPointF head = center + (normal.p2()-normal.p1()); // change '+' to '-' in order to point the arrow in other direction
    29. _arrow->_p1 = this->mapToItem(_arrow, center); // assume _p1 = arrow base, _p2 = arrow head
    30. _arrow->_p2 = this->mapToItem(_arrow, head);
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 
    Now you have two separate classes and you can literally attach arrow to the line item I think you can figure out the details but I'll be happy to help if not.

  8. #28
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    @Stampede - thanks for the code. But my requirement is slightly different from the code above.
    In the above code, the arrow line item is positioned on the parent line item at its center. The arrow line P1 is positioned at the center of the parent line. In this case the Arrow line rotates along with the parent line, when the parent line rotates since the Arrow line's P1 is on the axis of rotation of the Parent line.

    In my requirement, the Arrow line item's base P1 does not actually lie on the Parent line. The Arrow line's P1 and P2 points lie on either side of the parent line.
    In such a scenario, when rotating the parent line, the Arrow item gets displaced from its initial position each time we rotate the parent line.
    I do not know how to solve this problem. Please find the attched screen shot showing how the Arrow line is positioned with respect to the parent line.

    arrow_display_3.jpg

    Each time I roate the parent line and move it to new coordinate system, the Arrow line is getting displaced. My requirement is that Arrow line should remain in the same position wrt to parent line item no matter in which ever direction the parent line is rotated.

  9. #29
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    You can simply change the "center" point in the following code to whatever you need as a "base" point of attachement
    Qt Code:
    1. // this will attach arrow on the center of line item, but it is easy to change that with different 'center' point
    2. const QPointF center = (_p1+_p2)/2;
    To copy to clipboard, switch view to plain text mode 

  10. #30
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    @stampede - I tried very hard, I wish it were that simple. I spent considerably amount of time on this, and it doesnt really work.
    The Arrow line item rotates along with the parent "only if its base point lies any where on the parent line". The Arrow line starts getting displaced if it's "base" point is not exactly located on the parent line but is located just "below or above" the parent line . For example, if the base point is calculated as below, arrow item gets displaced from the parents axis as the parent is rotated.

    _p2 = _p2 + QPointF(0,20); // the calculation is just random, to place arrow base at above or below parent line.
    const QPointF center = (_p1+_p2)/2;

    Now if the arrow "base" point is located on the above center point, As the parent line is rotated eventually arrow line gets displaced from its initial position.
    If you test the whole code together, I am sure you will face this issue. Could you please combine the parent line code and arrow item code and check this behavior? My guess is that it is working if the parent line coordinate system is positive one but once iparent line starts rotating and switches to new coordinate system the center point calculations goes for a toss and doesn't hold good for the arrow position.

    Btw did you see the attached images in my previous post?

  11. #31
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    Qt Code:
    1. _p2 = _p2 + QPointF(0,20); // the calculation is just random, to place arrow base at above or below parent line.
    2. const QPointF center = (_p1+_p2)/2;
    To copy to clipboard, switch view to plain text mode 
    You have changed _p2 of "this" object in a place where you shouldn't, this method should only place the child line in correct position. In this method just use the existing _p1 and _p2 of the parent item to calculate new _p1 and _p2 of child, nothing more - look at the method name, "updateArrowItem", and not "updateMyPositionWithRandomData()".
    If you keep changing _p2 like that in this method I doubt you can predict the final result.
    Don't do "random" calculations, according to your specs it shouldn't be random. If you want to translate the base point for attachement, do that correctly using parent line normal vector, for example:
    Qt Code:
    1. void Line::updateArrowItem(){
    2. if (_arrow) {
    3. QLineF normal = QLineF(_p1,_p2).normalVector();
    4. // this will attach arrow sligthly below parent line, near the second point
    5. normal.setLength(10);
    6. const QPointF p = QLineF(_p1,_p2).pointAt(0.8);
    7. const QPointF center = p - (normal.p2()-normal.p1()); // move the base point below the parent line
    8. normal.setLength(30); // or whatever else arrow length you need
    9. const QPointF head = center + (normal.p2()-normal.p1()); // change '+' to '-' in order to point the arrow in other direction
    10. _arrow->_p1 = this->mapToItem(_arrow, center); // assume _p1 = arrow base, _p2 = arrow head
    11. _arrow->_p2 = this->mapToItem(_arrow, head);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    I have tried it and it works ok, you are very close to the solution.
    Last edited by stampede; 24th March 2014 at 19:54.

  12. The following user says thank you to stampede for this useful post:

    jeevan_ravula (27th March 2014)

  13. #32
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    @Stampede - Thanks a lot for all the help. I really learnt a lot interacting with u and its been pleasure talkin to u :-)

  14. #33
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    @Stampede : hii, offlate I have been working on resizing of a qgraphicspixmapitem which holds a png image in the shape of a rectangle. I am trying to resize the graphicspixmap item located on the scene. I have implemented the mouseMoveEvent() of my custom pix map item. I am only partly successful. I am able to resize the pixmap only when I resize from the right and bottom side of the rectangle. I am unable to resize when I place my mouse on the left and top side of the rectangle.
    Could you please look at my code and let me know what more needs to be done inorder to resize the rect from the top and left side of it?

    void PersonSizeGraphicsItem::mouseMoveEvent(QGraphicsSc eneMouseEvent *event)
    {
    const QPointF event_pos = event->pos();
    const QPointF event_scene_pos = event->scenePos();

    QPixmap current_pixmap = this->pixmap();
    QImage current_image = current_pixmap.toImage();
    QRect current_image_rect = current_image.rect();

    QPoint current_top_left = current_image_rect.topLeft();
    QPoint current_bottom_right = current_image_rect.bottomRight();

    if((event->scenePos().x() > this->scene()->width()) || (event->scenePos().y() > this->scene()->height())
    || (event->scenePos().x() < 0) || (event->scenePos().y() < 0) )
    {
    return;
    }

    if( this->cursor().shape() == Qt::SizeHorCursor )
    {
    QRect new_rect( current_top_left, QPoint( event->pos().x(), current_bottom_right.y()) );
    setPixmap(QPixmap::fromImage(current_image.scaled( QSize(new_rect.width(),new_rect.height()),Qt::Igno reAspectRatio,Qt::FastTransformation)));
    if(rect_left_condition)
    {
    QRect rect = this->pixmap().rect();
    rect.moveTo(event_scene_pos.x(), event_scene_pos.y));
    }

    }

    if( this->cursor().shape() == Qt::SizeVerCursor )
    {
    QRect new_rect( current_top_left, QPoint(current_bottom_right.x(), event->pos().y()) );
    setPixmap(QPixmap::fromImage(current_image.scaled( QSize(new_rect.width(),new_rect.height()),Qt::Igno reAspectRatio,Qt::FastTransformation)));
    if(rect_top_condition)
    {
    QRect rect = this->pixmap().rect();
    rect.moveTo(event_scene_pos.x(), event_scene_pos.y));
    }
    }

    this->update();

    }


    btw how do we attach files here? I couldn't figure out so I am posting only the implementation of mouseMoveEvent().

  15. #34
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    I guess you should start new thread, as this is not related to rotating a graphics line item.
    Another thing, don't address me directly in your questions, there are lots of people much more smarter than me, they could help you as well.
    btw how do we attach files here? I couldn't figure out so I am posting only the implementation of mouseMoveEvent().
    It is better to post code using CODE tags, much more convenient than downloading an attachement just to have a look at your source code.

  16. #35
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rotating a QGraphicsLineItem in a QGraphicsScene

    Thanks for your reply.I have taken note of your suggestions. I have figured out the problem related to resizing the rectangular pixmap item and fixed it.

    However I do have another issue related to qgraphicslineitem. When a parent qgraphicslineitem (containing arrow line graphics item as its child) rotates on either of its end points, how do we ensure that the child item does not go out of bounds on the scene, once the rotation of parent item is stopped?

    Currently I am checking the boundary condition for the parent item only by using the mouse move event's scene position. Since my arrow line item is positioned on the parent graphics line item, how can we handle boundary conditions for child item too. Is it possible to create a single bounding rect which covers both parent and children and then check the entire bounding rect position on the scene during mouse move?

    The code below shows the current implementation for checking the boundary condition for parent item on a scene.

    CustomGraphicsLineItem::mouseMoveEvent(QGraphicsSc eneMouseEvent *event)
    {

    if((event->scenePos().x() > this->scene()->width()) || (event->scenePos().y() > this->scene()->height()) ||
    (event->scenePos().x() < 0) || (event->scenePos().y() < 0) )
    {
    return;
    }

    const QPointF anchor = dragIndex == 0 ? this->line().p1() : this->line().p2();
    this->setLine(dragIndex == 0 ? QLineF(anchor, event->pos()) : QLineF(event->pos(),anchor));

    }
    Last edited by jeevan_ravula; 2nd April 2014 at 19:35.

Similar Threads

  1. QGraphicsLineItem - selection style
    By stefan in forum Qt Programming
    Replies: 5
    Last Post: 29th November 2010, 09:02
  2. Setting the QGraphicsLineItem tickness.....
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 9th July 2008, 12:11
  3. QGraphicsLineItem + setAcceptHoverEvents
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 12:13
  4. Rotating Printing of QGraphicsScene
    By init2null in forum Qt Programming
    Replies: 4
    Last Post: 22nd March 2008, 02:42
  5. Newbie needs advice - QGraphicsLineItem
    By Seth in forum Newbie
    Replies: 4
    Last Post: 30th May 2007, 08:23

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.