Results 1 to 10 of 10

Thread: Incorrect mouse behavior in GraphicsScene

  1. #1
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Incorrect mouse behavior in GraphicsScene

    I'm trying to draw my own objects(inherited from QGraphicsItem) on my own scene (inherited from QGraphicsScene) on mouse click. When I handle mousePressEvent it puts new object in the wrong place (namely it doubles its coordinates while setting and speed while moving).

    I handle MousePressEvent like this:
    Qt Code:
    1. void PolygonScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    2. {
    3. if (mouseEvent->button() != Qt::LeftButton)
    4. return;
    5.  
    6. PPoint *p = new PPoint(QColor(127, 0, 0), mouseEvent->scenePos().x(), mouseEvent->scenePos().y());
    7. addItem(p);
    8. QGraphicsScene::mousePressEvent(mouseEvent);
    9. }
    To copy to clipboard, switch view to plain text mode 

    And I create GraphicsScene and GraphicsView this way:
    Qt Code:
    1. graphScene = new PolygonScene();
    2. QPixmap pix = QPixmap::fromImage(image);
    3. graphScene->clear();
    4. graphScene->addPixmap(pix);
    5. ui.graphicsView->setGeometry(QRect(5, 5, pix.width(), pix.height()));
    6. ui.graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    7. ui.graphicsView->setScene(graphScene);
    8. ui.graphicsView->show();
    To copy to clipboard, switch view to plain text mode 

    Where should i look for the source of troube?
    Thank you

  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: Incorrect mouse behavior in GraphicsScene

    How does boundingRect() for your PPoint class look like? Please also post the constructor as it will probably be relevant too.
    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.


  3. #3
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Incorrect mouse behavior in GraphicsScene

    Here they are

    Qt Code:
    1. QRectF PPoint::boundingRect() const
    2. {
    3. return QRectF(x()-size, y()-size, x()+size, y()+size);
    4. }
    5.  
    6. QPainterPath PPoint::shape() const
    7. {
    8. path.addEllipse(boundingRect());
    9. return path;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. PPoint::PPoint(const QColor &color, int x, int y)
    2. {
    3. this->setX(x);
    4. this->setY(y);
    5. this->color = color;
    6. size = 15;
    7.  
    8. setFlags(ItemIsSelectable | ItemIsMovable);
    9. setAcceptHoverEvents(true);
    10. }
    11.  
    12. PPoint::PPoint() : QGraphicsItem()
    13. {
    14. color = QColor(0,0,255);
    15. size = 15;
    16. }
    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: Incorrect mouse behavior in GraphicsScene

    BoundingRect() of your class is invalid. It should return range of coordinates of the item regardless of the position of the item. In your situation it should probably be QRect(-size, -size, 2*size, 2*size) as I assume you meant the item to have the "anchor" in the center of the rectangle (and not in the bottom right corner as the current code would do (the last two parameters to QRect are the width and height of the rectangle and not the coordinates of its bottom right vertex) if we neglected the incorrect use of x() and y()) and "size" being the radius of the ellipse. Furthermore I'm assuming you didn't set the size of your scene which means it expands to envelop all items it's holding. If the scene is smaller than the view, it will by default be centered in the view which can also cause a virtual effect of translated coordinates.

    I'd change your PPoint class constructor and call to:
    Qt Code:
    1. void PolygonScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    2. {
    3. if (mouseEvent->button() != Qt::LeftButton)
    4. return;
    5.  
    6. PPoint *p = new PPoint(QColor(127, 0, 0));
    7. addItem(p);
    8. p->setPos(mouseEvent->pos());
    9. }
    10.  
    11. QRectF PPoint::boundingRect() const
    12. {
    13. return QRectF(-size, -size, 2*size, 2*size);
    14. }
    15.  
    16. PPoint::PPoint(const QColor &color)
    17. {
    18. this->color = color;
    19. size = 15;
    20. setFlags(ItemIsSelectable | ItemIsMovable);
    21. setAcceptHoverEvents(true);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Of course paint() should only paint within the coordinates returned by boundingRect() (i.e. [-size, size] in both directions).
    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.


  5. The following user says thank you to wysota for this useful post:

    framalex (11th June 2010)

  6. #5
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Incorrect mouse behavior in GraphicsScene

    I made all changes you suggested

    Set scene size:
    Qt Code:
    1. QPixmap pix = QPixmap::fromImage(image);
    2. graphScene->clear();
    3. graphScene->setSceneRect(0,0,pix.width(), pix.height());
    4. graphScene->addPixmap(pix);
    5. ui.graphicsView->setGeometry(QRect(5, 5, pix.width(), pix.height()));
    6. ui.graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    7. ui.graphicsView->setScene(graphScene);
    8. ui.graphicsView->show();
    To copy to clipboard, switch view to plain text mode 

    Constructor, boundingRect and mousePressEvent looks like you posted exactly.

    Here is my paint fuction:
    Qt Code:
    1. void PPoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
    2. {
    3. Q_UNUSED(widget);
    4. QBrush b = painter->brush();
    5. painter->setBrush(QBrush(color));
    6. painter->drawEllipse(0, 0, size, size);
    7. painter->setBrush(b);
    8. }
    To copy to clipboard, switch view to plain text mode 

    But now every time i click the mouse on GraphicsView it draws PPoint in the left upper corner. Strange...
    This behavior remains even when i write

    Qt Code:
    1. painter->drawEllipse(x(), y(), size, size);
    To copy to clipboard, switch view to plain text mode 

    as it was before

  7. #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: Incorrect mouse behavior in GraphicsScene

    If you changed boundingRect() to the one I posted then your paint() is incorrect. You are painting an ellipse that occupies only the right lower quarter of your item. The proper call would be:
    Qt Code:
    1. painter->drawEllipse(boundingRect());
    To copy to clipboard, switch view to plain text mode 

    I also suggest to replace the pixmap item you added to the scene with:
    Qt Code:
    1. graphScene->setBackgroundBrush(pix);
    To copy to clipboard, switch view to plain text mode 

    Also please use layouts to position your graphics view instead of manual calls to setGeometry().
    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.


  8. #7
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Incorrect mouse behavior in GraphicsScene

    Well, the problem still remains

    Here is the whole realization of PPoint class

    Qt Code:
    1. #include "ppoint.h"
    2.  
    3. PPoint::PPoint(const QColor &color, int x, int y)
    4. {
    5. this->setX(x);
    6. this->setY(y);
    7. this->color = color;
    8. size = 15;
    9.  
    10. setFlags(ItemIsSelectable | ItemIsMovable);
    11. setAcceptHoverEvents(true);
    12. }
    13.  
    14. PPoint::PPoint(const QColor &color) : QGraphicsItem()
    15. {
    16. this->color = color;
    17. size = 15;
    18. setFlags(ItemIsSelectable | ItemIsMovable);
    19. setAcceptHoverEvents(true);
    20. }
    21.  
    22. PPoint::~PPoint()
    23. {
    24.  
    25. }
    26.  
    27.  
    28. void PPoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
    29. {
    30. Q_UNUSED(widget);
    31. QBrush b = painter->brush();
    32. painter->setBrush(QBrush(color));
    33. painter->drawEllipse(boundingRect());
    34. painter->setBrush(b);
    35. }
    36.  
    37. void PPoint::mousePressEvent(QGraphicsSceneMouseEvent *event)
    38. {
    39. QGraphicsItem::mousePressEvent(event);
    40. update();
    41. }
    42.  
    43. void PPoint::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    44. {
    45. QGraphicsItem::mouseMoveEvent(event);
    46. if (event->modifiers() & Qt::ShiftModifier) {
    47. update();
    48. return;
    49. }
    50.  
    51. }
    52.  
    53. void PPoint::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    54. {
    55. QGraphicsItem::mouseReleaseEvent(event);
    56. update();
    57. }
    58.  
    59. QRectF PPoint::boundingRect() const
    60. {
    61. return QRectF(-size, -size, 2*size, 2*size);
    62. }
    63.  
    64. QPainterPath PPoint::shape() const
    65. {
    66. path.addEllipse(boundingRect());
    67. return path;
    68. }
    To copy to clipboard, switch view to plain text mode 

    and PolygonScene

    Qt Code:
    1. PolygonScene::PolygonScene(QObject *parent)
    2. : QGraphicsScene(parent)
    3. {
    4. }
    5.  
    6. PolygonScene::~PolygonScene()
    7. {
    8.  
    9. }
    10.  
    11. void PolygonScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    12. {
    13. if (mouseEvent->button() != Qt::LeftButton)
    14. return;
    15.  
    16. PPoint *p = new PPoint(QColor(127, 0, 0));
    17. addItem(p);
    18. p->setPos(mouseEvent->pos());
    19. // QGraphicsScene::mousePressEvent(mouseEvent);
    20. }
    21.  
    22. void PolygonScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
    23. {
    24.  
    25. QGraphicsScene::mouseMoveEvent(mouseEvent);
    26. }
    27.  
    28. void PolygonScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
    29. {
    30. QGraphicsScene::mouseReleaseEvent(mouseEvent);
    31. }
    To copy to clipboard, switch view to plain text mode 

  9. #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: Incorrect mouse behavior in GraphicsScene

    Here is a working example:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class PPoint : public QGraphicsItem {
    4. public:
    5. PPoint(const QColor &c) : QGraphicsItem(), m_size(15), m_color(c){}
    6. QRectF boundingRect() const { return QRectF(-m_size, -m_size, 2*m_size, 2*m_size); }
    7. QPainterPath shape() const { QPainterPath p; p.addEllipse(boundingRect()); return p; }
    8.  
    9. void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) {
    10. painter->setBrush(m_color);
    11. painter->drawEllipse(boundingRect());
    12. if(option->state & QStyle::State_Selected) {
    13. painter->setBrush(Qt::NoBrush);
    14. painter->setPen(Qt::yellow);
    15. painter->drawRect(boundingRect());
    16. }
    17. }
    18. private:
    19. int m_size;
    20. QColor m_color;
    21. };
    22.  
    23. class MyScene : public QGraphicsScene {
    24. public:
    25. MyScene(const QPixmap &px, QObject *parent = 0) : QGraphicsScene(parent) {
    26. setSceneRect(px.rect());
    27. setBackgroundBrush(px);
    28. }
    29. protected:
    30. void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent ) {
    31. if(itemAt(mouseEvent->scenePos())){
    32. QGraphicsScene::mousePressEvent(mouseEvent);
    33. return;
    34. }
    35. PPoint *item = new PPoint(Qt::darkRed);
    36. addItem(item);
    37. item->setPos(mouseEvent->scenePos());
    38. item->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsSelectable);
    39. }
    40. };
    41.  
    42. int main(int argc, char **argv){
    43. QApplication app(argc, argv);
    44. QPixmap px("/usr/share/wallpapers/default_blue.jpg");
    45. view.setScene(new MyScene(px, &view));
    46. view.show();
    47. return app.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 
    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.


  10. The following user says thank you to wysota for this useful post:

    framalex (11th June 2010)

  11. #9
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Incorrect mouse behavior in GraphicsScene

    Thank you!


    My last code had only one error - I used
    mouseEvent->pos()
    instead of
    mouseEvent->scenePos()
    Now it works

  12. #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: Incorrect mouse behavior in GraphicsScene

    Seems that QGraphicsSceneMouseEvent::pos() returns a null point when called from within the scene. I think it's a bit illogical, it should be consistent with QGraphicsItem::pos().
    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. Incorrect Julian Day
    By Rayven in forum Qt Programming
    Replies: 6
    Last Post: 16th November 2011, 13:10
  2. Suggestions for drawing with mouse on GraphicsScene
    By GimpMaster in forum Qt Programming
    Replies: 7
    Last Post: 2nd April 2010, 18:44
  3. transfer graphicsscene on form1 to graphicsscene on form2
    By rogerholmes in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2009, 20:37
  4. Changing mouse grabbing behavior?
    By FlyingSaucrDude in forum Qt Programming
    Replies: 0
    Last Post: 11th November 2008, 01:04
  5. incorrect QGraphicsItemGroup mouse drag behavior
    By Gopala Krishna in forum Qt Programming
    Replies: 1
    Last Post: 25th January 2008, 10:05

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.