Results 1 to 11 of 11

Thread: Problems with QGraphicsItem and Overiding methods

  1. #1

    Default Problems with QGraphicsItem and Overiding methods

    Hi,

    My setup, I have a
    QGraphicsView Object
    QGraphicsScene Object
    and a controller object...

    The QGraphicsScene returns QGraphicItems usually.

    I have a hierachi, where I have created DGraphicRectItems, which derive from QGraphicRectItems.

    DGraphicRectItems have mouse methods such as MousePressEvent(); which should overide the QGraphicsRectItems default handling.

    However when I press or hover or anything over the DGraphicsRectItem objects on the canvas, nothing happens.

    I suspect this is because the mouseEvents are being sent to the QGraphicRectItems since this is what the scene deals with......

    But this should not be the case, i should be able to have those D mouse methods run.

    Could sombody explain why this is the case?

    I have even tried writing mouseEvent->accept(); inside the D mouse method. still nothing happens.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with QGraphicsItem and Overiding methods

    Could you show the DGraphicsRectItem::mousePressEvent() function declaration? Notice that it's mousePressEvent() not MousePressEvent().
    J-P Nurmi

  3. #3

    Default Re: Problems with QGraphicsItem and Overiding methods

    Qt Code:
    1. #pragma once
    2. #include <QtGui/QGraphicsRectItem>
    3. #include "DGraphicsItem.h"
    4. class DGraphicsRectItem :
    5. public QGraphicsRectItem, public DGraphicsItem
    6. {
    7. public:
    8. DGraphicsRectItem(void);
    9. ~DGraphicsRectItem(void);
    10. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    11. void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
    12. void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    13. void changeSize(QPointF *change);
    14. void instantiateDescriptors();
    15. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "DGraphicsRectItem.h"
    2. #include <iostream>
    3. #include <QPointF>
    4. #include <QtGui/QGraphicsSceneMouseEvent>
    5. DGraphicsRectItem::DGraphicsRectItem(void)
    6. {
    7. this->setFlag(DGraphicsRectItem::ItemIsSelectable,true);
    8. this->setFlag(DGraphicsRectItem::ItemIsMovable,true);
    9. this->setFlag(DGraphicsRectItem::ItemIsFocusable,true);
    10.  
    11. }
    12.  
    13. DGraphicsRectItem::~DGraphicsRectItem(void)
    14. {
    15. }
    16.  
    17. void DGraphicsRectItem::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    18. {
    19. // QGraphicsRectItem::mousePressEvent(mouseEvent);
    20. std::cout << "DGraphicsRectItem::mousePressEvent";
    21. }
    22. void DGraphicsRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent){
    23. std::cout << "DGraphicsRectItem::mouseMoveEvent";
    24. }
    25.  
    26. void DGraphicsRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent){
    27. std::cout << "releaseevent";
    28. }
    29.  
    30. void DGraphicsRectItem::changeSize(QPointF *change)
    31. {
    32.  
    33. std::cout << "YES" << std::endl;
    34. }
    35.  
    36. void DGraphicsRectItem::instantiateDescriptors()
    37. {
    38. std::cout << "DGraphicsRectItem::instantiateDescriptors()" << std::endl;
    39. //DescRect *item = new DescRect();
    40.  
    41. //instantiate rect descriptor.
    42. //create modify event object
    43.  
    44. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with QGraphicsItem and Overiding methods

    How do you know DGraphicsRectItem::mousePressEvent() doesn't get called? Perhaps it's just the missing std::endl...
    J-P Nurmi

  5. #5

    Default Re: Problems with QGraphicsItem and Overiding methods

    I have set a breakpoint.

    I should also say that I have overidden GraphicsScene::mouseMoveEvent events.
    And these events ARE triggered. since the breakpoint for these events are hit.

    I have forwarded the event to another function, and this other function 'ignores the event'.

    So the scene mousemove events are being triggered, just not the DGraphicRectItem mouseMove events
    Last edited by Terabyte; 27th December 2008 at 17:18.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with QGraphicsItem and Overiding methods

    If you override QGraphicsScene mouse event handlers, you must call the base class implementation if you want any item to receive mouse events.
    J-P Nurmi

  7. #7

    Default Re: Problems with QGraphicsItem and Overiding methods

    Quote Originally Posted by jpn View Post
    If you override QGraphicsScene mouse event handlers, you must call the base class implementation if you want any item to receive mouse events.
    how do i do that?
    and where do i do that?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with QGraphicsItem and Overiding methods

    Qt Code:
    1. void SubClass::virtualFunction()
    2. {
    3. BaseClass::virtualFunction(); // <---
    4. // ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9

    Default Re: Problems with QGraphicsItem and Overiding methods

    where subclass is my subclass of QGraphicsScene
    in this case called BoardModel

    and BaseClass is QGraphicsScene itself?


    and virtualfunction is mouseMoveEvent?

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with QGraphicsItem and Overiding methods

    Yes, exactly. Your reimplementation is called instead of the QGraphicsScene implementation, but the QGraphicsScene implementation is the one who delivers mouse events to the items.
    J-P Nurmi

  11. #11

    Default Re: Problems with QGraphicsItem and Overiding methods

    Quote Originally Posted by jpn View Post
    Yes, exactly. Your reimplementation is called instead of the QGraphicsScene implementation, but the QGraphicsScene implementation is the one who delivers mouse events to the items.
    thank you, it worked. Spent months wondering about that!

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.