Results 1 to 6 of 6

Thread: Drawing a fixed-sized shape at mouse press position on QGraphicsView

  1. #1
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drawing a fixed-sized shape at mouse press position on QGraphicsView

    Hi, I am trying to write an application to manipulate images using the GraphicsView framework.
    I derived QGraphicsPixmapItem to display an image and reimplemented mousePressEvent() to draw a circle at the mouse press position on the image.
    I want the image to automatically fit to the window when the view is resized while keeping the circle size fixed.
    So I set the ItemIgnoresTransformations flag in order not to resize the circle, and converted the mouse press position to the view coordinate before setting it as the circle position.
    But the position of the drawn circle is deviated from the mouse press position.

    The following is the simplified code of mine.

    Qt Code:
    1. #include <QApplication>
    2. #include <QtCore>
    3. #include <QtGui>
    4.  
    5. class MyItem : public QGraphicsPixmapItem
    6. {
    7. public:
    8. MyItem(QGraphicsView *v): view(v)
    9. {
    10. circle = new QGraphicsPathItem(this);
    11. circle->setFlag(QGraphicsItem::ItemIgnoresTransformations);
    12. circle->hide();
    13. }
    14.  
    15. protected:
    16. virtual void mousePressEvent(QGraphicsSceneMouseEvent * event)
    17. {
    18. QPointF pos_view = view->mapFromScene(mapToScene(event->pos()));
    19. const int Radius = 100;
    20. path.addEllipse(pos_view, Radius, Radius);
    21. circle->setPath(path);
    22. circle->show();
    23. update();
    24. }
    25.  
    26. private:
    27. };
    28.  
    29. class MainWindow : public QMainWindow
    30. {
    31. public:
    32. MainWindow()
    33. {
    34. view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    35. view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    36. view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    37.  
    38. setCentralWidget(&view);
    39. setWindowTitle(tr("Image Viewer"));
    40.  
    41. item = new MyItem(&view);
    42. item->setPixmap(QPixmap("test.tif"));
    43. item->setTransformationMode(Qt::SmoothTransformation);
    44.  
    45. scene.addItem(item);
    46. view.setScene(&scene);
    47. }
    48.  
    49. protected:
    50. virtual void resizeEvent(QResizeEvent *event)
    51. {
    52. view.fitInView(item, Qt::KeepAspectRatio);
    53. }
    54.  
    55. private:
    56. MyItem *item;
    57. };
    58.  
    59.  
    60. int main(int argc, char *argv[])
    61. {
    62. QApplication app(argc, argv);
    63. MainWindow mw;
    64. mw.show();
    65. mw.resize(500, 700);
    66. return app.exec();
    67. }
    To copy to clipboard, switch view to plain text mode 

    Could someone tell me what is wrong with this?

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

    Default Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView

    Why do you map to and back from scene coordinates?
    You are taking your item coordinates, map them to scene coords, and than map that to view coords, and you give these coords to the item to draw!
    The position you get from the event is in item coordinates, and you are in an item, so just use the position you get from the event.
    ==========================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
    Apr 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView

    Hi, thank you very much for reply.

    Quote Originally Posted by high_flyer View Post
    You are taking your item coordinates, map them to scene coords, and than map that to view coords, and you give these coords to the item to draw!
    The position you get from the event is in item coordinates, and you are in an item, so just use the position you get from the event.
    But if I just use the position given from the event, the item is not drawn at the mouse press position because I set QGraphicsItem::ItemIgnoresTransformations flag of the item to prevent it from resizing with window...
    Is there any better way to keep the item size to fixed number of pixels?

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

    Default Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView

    the item is not drawn at the mouse press position because I set QGraphicsItem::ItemIgnoresTransformations flag
    I don't see why this should matter?
    The flag means that the item doesn't scale.
    But its position stays the same in the parent coordinates, and the item coordinates remain the item coordinates.
    ==========================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
    Apr 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView

    Thank you again for your reply.

    Quote Originally Posted by high_flyer View Post
    I don't see why this should matter?
    The flag means that the item doesn't scale.
    But its position stays the same in the parent coordinates, and the item coordinates remain the item coordinates.
    The item is drawn at the mouse press position if I don't set the QGraphicsItem::ItemIgnoresTransformations, but NOT if the flag is set...
    I would appreciate it if you could confirm this using my code above with any image named test.tif.

  6. #6
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView

    I've found a kind of solution.
    If I make the item a direct child of the scene, it is drawn at the mouse press position.
    Do I need to define the transformation matrix by myself to make the item a child of another item?
    Seems tricky...

Similar Threads

  1. Problem with mouse press on QGraphicsView.
    By pastispast in forum Qt Programming
    Replies: 10
    Last Post: 13th September 2010, 06:52
  2. QGraphicsview and mouse press event
    By eva2002 in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2010, 05:04
  3. How to make a QMainWindow fixed sized?
    By Goldmmbr in forum Newbie
    Replies: 2
    Last Post: 18th November 2009, 08:52
  4. How to make headers fixed sized? (QTableWidget)
    By macias in forum Qt Programming
    Replies: 4
    Last Post: 13th August 2007, 15:57
  5. Draw QtCanvasElipse on mouse press event position
    By YuriyRusinov in forum Newbie
    Replies: 1
    Last Post: 31st May 2006, 11:57

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.