+ Reply to Thread
Results 1 to 8 of 8

Thread: Problem getting mouse events from overrider QGraphicsPixmapItem

  1. #1
    Join Date
    Jan 2011
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem getting mouse events from overrider QGraphicsPixmapItem

    Hello,

    I would like to use my own implementation of QGraphicsPixmapItem so that I can do things during the mouse events like zoom the image, etc.

    here is my class:

    Qt Code:
    1. #include <QPainter>
    2. #include "imagepixmapitem.h"
    3. #include <QGraphicsSceneWheelEvent>
    4.  
    5. ImagePixmapItem::ImagePixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem)
    6. : QGraphicsPixmapItem(pixmap,parentItem)
    7. {
    8. setCacheMode(NoCache);
    9. setAcceptHoverEvents(true);
    10. setFlag(QGraphicsItem::ItemIsSelectable,true);
    11.  
    12. }
    13.  
    14. ImagePixmapItem::~ImagePixmapItem()
    15. {
    16. }
    17.  
    18. QPainterPath ImagePixmapItem::shape() const
    19. {
    20. QPainterPath painterPath;
    21. painterPath.addRect(boundingRect());
    22. return painterPath;
    23. }
    24.  
    25. void ImagePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event){
    26. qDebug("hello");
    27.  
    28. }
    29.  
    30. void ImagePixmapItem::wheelEvent ( QGraphicsSceneWheelEvent * event ){
    31. qDebug("Print this line if catch a wheelEvent");//this is never printing
    32. qreal factor = 1.2;
    33. if (event->delta() < 0)
    34. factor = 1.0 / factor;
    35. scale(factor, factor);
    36.  
    37. }
    To copy to clipboard, switch view to plain text mode 

    And here is where I create a new ImagePixmapItem:

    Qt Code:
    1. QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));
    2. ImagePixmapItem *item = static_cast<ImagePixmapItem *>(scene.addPixmap(p));
    3.  
    4. ui->graphicsView->setScene(&scene);
    5. ui->graphicsView->show();
    To copy to clipboard, switch view to plain text mode 

    where qi is my image.

    This loads correctly intot he graphicsView, but it does not seem to give me any of the mouse debug outputs.

    Any ideas what is wrong?
    Last edited by high_flyer; 21st January 2011 at 18:37. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,317
    Thanks
    20
    Thanked 377 Times in 371 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting mouse events from overrider QGraphicsPixmapItem

    you should call accept() on the event in the event handler.
    Recommended off-line reading:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  3. #3
    Join Date
    Jan 2011
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem getting mouse events from overrider QGraphicsPixmapItem

    Did you mean in the ImagePixmapItem? I dont think those functions ever get called - the qDebug statements never print

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,317
    Thanks
    20
    Thanked 377 Times in 371 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting mouse events from overrider QGraphicsPixmapItem

    From the docs:
    An item becomes a mouse grabber when it receives and accepts a mouse press event, and it stays the mouse grabber until either of the following events occur:

    * If the item receives a mouse release event when there are no other buttons pressed, it loses the mouse grab.
    * If the item becomes invisible (i.e., someone calls item->setVisible(false)), or if it becomes disabled (i.e., someone calls item->setEnabled(false)), it loses the mouse grab.
    * If the item is removed from the scene, it loses the mouse grab.

    If the item loses its mouse grab, the scene will ignore all mouse events until a new item grabs the mouse (i.e., until a new item receives a mouse press event)
    although I guess the debug message should be printed any way...
    Check if your item is a mouse grabber before you click it.
    Recommended off-line reading:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  5. #5
    Join Date
    Jan 2011
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem getting mouse events from overrider QGraphicsPixmapItem

    So I added item->grabMouse() right after I define the item. I am still not getting those debug statements. Do i need to do anything with setting the sceneRect or item rect or anything?

    Incidentally, when I do try to scroll up and down with the wheel, my graphicsview scrolls up or down. Is it possibly intercepting my mouse calls and not passing them to the item? It is just a normal QGraphicsView, but I did turn on the ScrollHandDrag mode.

    Nothing at all happens when I click anywhere on the image

  6. #6
    Join Date
    Dec 2009
    Posts
    126
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting mouse events from overrider QGraphicsPixmapItem

    Quote Originally Posted by dcole View Post
    It is just a normal QGraphicsView, but I did turn on the ScrollHandDrag mode.
    Did you check the interactive property of your view ? i.e. : QGraphicsView::setInteractive(bool)

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,317
    Thanks
    20
    Thanked 377 Times in 371 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting mouse events from overrider QGraphicsPixmapItem

    hmm.. what does mouseGrabberItem () return?
    Recommended off-line reading:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  8. #8
    Join Date
    Jan 2011
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem getting mouse events from overrider QGraphicsPixmapItem

    Turns out it was a problem with the reference to item. I had to define an item instance first, and then use the scene.addItem() method to put my custom item on there. After I did this, the problem was solved.

    Thanks for the help!

+ Reply to Thread

Similar Threads

  1. problem propagating mouse events.
    By ngc_1729 in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2010, 15:11
  2. Mouse events in QGraphicsPixmapItem and QGraphicsTextItem
    By manojmka in forum Qt Programming
    Replies: 3
    Last Post: 8th August 2008, 12:38
  3. Problem in Mouse Press Events & Vectors
    By dheeraj in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2008, 19:08
  4. problem about mouse button events
    By vql in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2008, 11:14
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 07:13

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