Results 1 to 6 of 6

Thread: resize and rotate handle

  1. #1
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default resize and rotate handle

    Hi I am trying make some resize and rotate handle of each item of class QGraphicsItem, but I have problem with it. I select my item, but I dont know what I should do to rotate or resize my item Could someone give me some hint, what I can use?
    I tried it with helpful QRect and rotate with QGraphicsItem::rotate() and QGraphicsItem::scaled() but It doesnt work according to my idea before

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: resize and rotate handle

    hi,
    please send the source.

  3. #3
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default Re: resize and rotate handle

    hi i solve it, but only partly
    Qt Code:
    1. HandleItem::HandleItem( QGraphicsItem *item, QGraphicsScene *scene,
    2. QColor color, HandleItem::HandleRole role,
    3. QList<HandleItem*> handles ) : QGraphicsItem( 0, scene )
    4. {
    5. m_role = role;
    6. m_color = color;
    7. m_item = item;
    8. m_handles = handles;
    9. m_pressed = false;
    10. setZValue( 100 );
    11. setFlag( ItemIsMovable );
    12. }
    13.  
    14. QRectF HandleItem::boundingRect() const
    15. {
    16. QPointF point = m_item->boundingRect().center();
    17. switch( m_role ){
    18. case CenterHandle:
    19. return QRectF( point-QPointF(5, 5), QSize( 10, 10 ) );
    20. case RightHandle:
    21. point.setX( m_item->boundingRect().right() );
    22. return QRectF( point-QPointF(3, 5), QSize( 6, 10 ) );
    23. case TopHandle:
    24. point.setY( m_item->boundingRect().top() );
    25. return QRectF( point-QPointF(5, 3), QSize( 10, 6 ) );
    26. }
    27. return QRectF();
    28. }
    29.  
    30. void HandleItem::paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget )
    31. {
    32. paint->setPen( m_color );
    33. paint->setBrush( m_color );
    34. QRectF rect = boundingRect();
    35. QVector<QPointF> points;
    36. switch( m_role ){
    37. case CenterHandle:
    38. paint->drawEllipse( rect );
    39. break;
    40. case RightHandle:
    41. points << rect.center()+QPointF(3,0) << rect.center()+QPointF(-3,-5) << rect.center()+QPointF(-3,5);
    42. paint->drawConvexPolygon( QPolygonF(points) );
    43. break;
    44. case TopHandle:
    45. points << rect.center()+QPointF(0,-3) << rect.center()+QPointF(-5,3) << rect.center()+QPointF(5,3);
    46. paint->drawConvexPolygon( QPolygonF(points) );
    47. break;
    48. }
    49. }
    50.  
    51. void HandleItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
    52. {
    53. m_pressed = true;
    54. QGraphicsItem::mousePressEvent( event );
    55. }
    56. void HandleItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
    57. {
    58. m_pressed = false;
    59. QGraphicsItem::mouseReleaseEvent( event );
    60. }
    61.  
    62. QVariant HandleItem::itemChange( GraphicsItemChange change, const QVariant &data )
    63. {
    64. if( change == ItemPositionChange && m_pressed ){
    65. QPointF movement = data.toPoint() - pos();
    66. QPointF center = m_item->boundingRect().center();
    67. switch( m_role ){
    68. case CenterHandle:
    69. m_item->moveBy( movement.x(), movement.y() );
    70. foreach( HandleItem *handle, m_handles )
    71. handle->translate( movement.x(), movement.y() );
    72. break;
    73. case TopHandle:
    74. if( -2*movement.y() + m_item->sceneBoundingRect().height() <= 5 )
    75. return QGraphicsItem::itemChange( change, pos() );
    76. movement.setX( 0 );
    77. m_item->translate( center.x(), center.y() );
    78. m_item->scale( 1, 1.0-2.0*movement.y() /(m_item->sceneBoundingRect().height()) );
    79. m_item->translate( -center.x(), -center.y() );
    80. break;
    81. case RightHandle:
    82. if( 2*movement.x() + m_item->sceneBoundingRect().width() <= 5 )
    83. return QGraphicsItem::itemChange( change, pos() );
    84. movement.setY( 0 );
    85. m_item->translate( center.x(), center.y() );
    86. m_item->scale( 1.0+2.0*movement.x()/(m_item->sceneBoundingRect().width()), 1 );
    87. m_item->translate( -center.x(), -center.y() );
    88. break;
    89. }
    90. return QGraphicsItem::itemChange( change, pos()+movement );
    91. }
    92. return QGraphicsItem::itemChange( change, data );
    93. }
    To copy to clipboard, switch view to plain text mode 
    class:
    Qt Code:
    1. class HandleItem : public QGraphicsItem
    2. {
    3. public:
    4. enum HandleRole { CenterHandle, RightHandle, TopHandle };
    5.  
    6. HandleItem( QGraphicsItem *item, QGraphicsScene *scene,
    7. QColor color, HandleRole role = CenterHandle,
    8. QList<HandleItem*> handles = QList<HandleItem*>() );
    9.  
    10. void paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget );
    11. QRectF boundingRect() const;
    12.  
    13. protected:
    14. void mousePressEvent( QGraphicsSceneMouseEvent *event );
    15. void mouseReleaseEvent( QGraphicsSceneMouseEvent *event );
    16. QVariant itemChange( GraphicsItemChange change, const QVariant &data );
    17.  
    18. private:
    19. QGraphicsItem *m_item;
    20. HandleRole m_role;
    21. QColor m_color;
    22. QList<HandleItem*> m_handles;
    23. bool m_pressed;
    24. };
    To copy to clipboard, switch view to plain text mode 
    It is OK, but I dont know how translate coordition system to my item has CenterHandle, TopHandle and RightHandle at same pocition...
    When I get mouseClick into my item CenterHandle, TopHandle and RightHandle are set well, I get mouse click next to my item CenterHandle, TopHandle and RightHandle are removed. It is OK. If I get mouseClick into my item CenterHandle, TopHandle and RightHandle are set and then I want to move my item. It is OK too. Then I click next to my item. It is OK. Then I want to move my item again, so I click into my item, but CenterHandle, TopHandle and RightHandle are set at first position(so I isnt in my item)

    So I would like to ask, how I set translate or something else my item, when I change position of item.

    Thanks a lot and sorry for my simple english

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: resize and rotate handle

    There is some Photo Wall application on Qt-apps.org
    You can search for it and see the code. It had provided good means to rotate and resize graphics item.

  5. #5
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default Re: resize and rotate handle

    wow it is great thanks I try it

  6. #6
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default Re: resize and rotate handle

    I solved it only partly, but when I try to resize QGraphicsTextItem, i show handles in position 0,0 of all scene

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.