Results 1 to 10 of 10

Thread: How to move my own QCanvasItem?

  1. #1
    Join Date
    Mar 2006
    Posts
    6
    Qt products
    Qt3
    Platforms
    Windows

    Default How to move my own QCanvasItem?

    Hi, everyone,

    I have a CircleItem class which inherits QCanvasEllipse. I reimplement areaPoints() and drawShape(). When I'm trying to move CircleItem, it doesn't work.

    Why? what else should I do?

    Qt Code:
    1. /////////////////////////////////////////////////////////////////////////
    2. class CircleItem : public QCanvasEllipse
    3. {
    4. public:
    5. CircleItem( int cx, int cy, int r, QCanvas *c ) : QCanvasEllipse( c )
    6. {
    7. this->cx = cx;
    8. this->cy = cy;
    9. this->r = r;
    10. }
    11.  
    12. ~CircleItem()
    13. {
    14. hide();
    15. }
    16.  
    17. QPointArray areaPoints() const
    18. {
    19. QPointArray res(4);
    20. res.setPoint( 0, QPoint( cx-r, cy-r ) );
    21. res.setPoint( 1, QPoint( cx-r, cy+r ) );
    22. res.setPoint( 2, QPoint( cx+r, cy+r ) );
    23. res.setPoint( 3, QPoint( cx+r, cy-r ) );
    24. return res;
    25. }
    26.  
    27.  
    28. protected:
    29. void drawShape( QPainter &p )
    30. {
    31. p.drawEllipse( cx-r, cy-r, 2*r, 2*r );
    32. }
    33.  
    34. private:
    35. int cx;
    36. int cy;
    37. int r;
    38. };
    39.  
    40. /////////////////////////////////////////////////////////////////////////
    To copy to clipboard, switch view to plain text mode 


    Here are two methods that draw a circle on MyCanvasView. The first one uses QCanvasEllipse, it works fine. The second uses my CircleItem, it doesn't work.

    Qt Code:
    1. void MyCanvasView::drawCircle-1()
    2. {
    3. QPoint p0 = inverseWorldMatrix().map(points[0]);
    4. QPoint p1 = inverseWorldMatrix().map(points[1]);
    5.  
    6. int r = (int) sqrt( pow(p0.x()-p1.x(),2)+pow(p0.y()-p1.y(),2) );
    7.  
    8. QCanvasPolygonalItem* i = new QCanvasEllipse(2*r, 2*r, canvas());;
    9. i->move(p0.x(), p0.y());
    10. i->setBrush( Qt::red ); ***********
    11. i->show();
    12.  
    13. }
    14.  
    15.  
    16. void MyCanvasView::drawCircle-2()
    17. {
    18. QPoint p0 = inverseWorldMatrix().map(points[0]);
    19. QPoint p1 = inverseWorldMatrix().map(points[1]);
    20.  
    21. int r = (int) sqrt( pow(p0.x()-p1.x(),2)+pow(p0.y()-p1.y(),2) );
    22.  
    23. QCanvasPolygonalItem* i = new CircleItem(p0.x(), p0.y(), r, canvas());;
    24. i->setPen(ROIPEN); ***********
    25. i->show();
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 


    The Doc says:
    Note: QCanvasEllipse does not use the pen.
    In my subclass, I used setPen(), because I need to draw the outline of the circle instead of filling it.

    Maybe this is the cause?

    Can anyone help?

    Thanks!

  2. #2
    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 move my own QCanvasItem?

    How do you try to move that item? Make sure boundingRect() returns a correct rectangle, because that's what is used to check for collisions.

  3. #3
    Join Date
    Mar 2006
    Posts
    6
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: How to move my own QCanvasItem?

    Thank you, wysota,

    Do you mean that I have to reimplement boundingRect() method?

    I think the area of areaPoints() is big enough.

    Here are two other methods:

    Qt Code:
    1. void MyCanvasView::contentsMousePressEvent( QMouseEvent * e )
    2. {
    3. // moving
    4. QPoint p = inverseWorldMatrix().map(e->pos());
    5. QCanvasItemList l=canvas()->collisions(p);
    6. for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
    7. if ( (*it)->rtti() == QCanvasItem::Rtti_Ellipse ) {
    8. moving = (QCanvasEllipse*) (*it);
    9. moving_start = p;
    10. break;
    11. } else {
    12. continue;
    13. }
    14. }
    15.  
    16. }
    17.  
    18. void MyCanvasView::contentsMouseMoveEvent( QMouseEvent *e )
    19. {
    20. // moving
    21. if ( moving ) {
    22. QPoint p = inverseWorldMatrix().map(e->pos());
    23. moving->moveBy(p.x() - moving_start.x(),
    24. p.y() - moving_start.y());
    25. moving_start = p;
    26. canvas()->update();
    27. }
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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 move my own QCanvasItem?

    And if you use QCanvasEllipse objects does the move work? And when you substitute them with your subclass they're not collided? Make a check in that for loop to list all the colliding objects to the console.

  5. #5
    Join Date
    Mar 2006
    Posts
    6
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: How to move my own QCanvasItem?

    And if you use QCanvasEllipse objects does the move work? And when you substitute them with your subclass they're not collided? Make a check in that for loop to list all the colliding objects to the console.
    If I use QCanvasEllipse, I can move the circle (filling with QBrush);

    If I use the subclass, I cann't move the circle (only outline). But, the object is really in the QCanvasItemList. (I tried to delete the circle within the for loop, it succeeds).


    Qt Code:
    1. void MyCanvasView::contentsMousePressEvent( QMouseEvent * e )
    2. {
    3. // moving
    4. QPoint p = inverseWorldMatrix().map(e->pos());
    5. QCanvasItemList l=canvas()->collisions(p);
    6. for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
    7. if ( (*it)->rtti() == QCanvasItem::Rtti_Ellipse ) {
    8. delete (*it); /////////////////// it works
    9. break;
    10. } else {
    11. continue;
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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 move my own QCanvasItem?

    If I use the subclass, I cann't move the circle (only outline).
    Could you explain that? What outline? Could you post a screenshot?

  7. #7
    Join Date
    Mar 2006
    Posts
    6
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: How to move my own QCanvasItem?

    Sorry, I don't know how to post a picture.

    The outline is only the circle created by drawShape() in class CircleItem.

  8. #8
    Join Date
    Mar 2006
    Posts
    6
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: How to move my own QCanvasItem?

    Here are screenshots.





  9. #9
    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 move my own QCanvasItem?

    Can you move() the item manually? Check it please... my guess is that it'll not be possible because you don't change values of cx, cy and r anywhere. You should probably reimplement moveBy() or better yet change your drawShape (and probably other methods too) to use x() and y() values. More or less like so:

    Qt Code:
    1. void CircleItem::drawShape( QPainter &p ){
    2. p.drawEllipse( x()-r, y()-r, 2*r, 2*r );
    3. }
    To copy to clipboard, switch view to plain text mode 

    Use setX() and setY() in your constructor to initialise those values with cx and cy. Of course areaPoints() will need to be changed to use x() and y() too.

  10. #10
    Join Date
    Mar 2006
    Posts
    6
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: How to move my own QCanvasItem?

    Finally, it worked!!


    Use setX() and setY() in your constructor to initialise those values with cx and cy. Of course areaPoints() will need to be changed to use x() and y() too.

    You are absolutely right!

    Thank you so much indeed!

    Have a nice day!

Similar Threads

  1. Move items up and down in QListWidget
    By araglin in forum Newbie
    Replies: 7
    Last Post: 31st August 2016, 11:05
  2. Replies: 1
    Last Post: 22nd November 2008, 06:32
  3. Move and resize with layout
    By waediowa in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2008, 08:16
  4. Move Rectangle on mouse Move
    By vermarajeev in forum Qt Programming
    Replies: 24
    Last Post: 14th May 2007, 05:34
  5. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28

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.