I am using following code to display a range of rectangles that can be clicked. Once clicked or hovered, first it should be highlighted and secondly some operation may be performed as well. But my program could not even highlight when i hover or press mouse button.

Following is the bits of code that may have the problem. Its a little lengthy but very easy code. Please review it and tell me if you have some suggestions for me...Thanks a lot

Qt Code:
  1. //MainWindow.cpp
  2. void MainWindow::drawMap()
  3. {
  4. scene->setSceneRect(-360,-266,1000,532);
  5. //scene->setItemIndexMethod(QGraphicsScene::NoIndex);
  6. for (int i = 0; i < 50; ++i) {
  7. RectMap *rectangle = new RectMap(50);
  8. rectangle->setPos(xLocation,0);
  9. xLocation = xLocation + 50 + 4 ;
  10. scene->addItem(rectangle);
  11. }
  12. //graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
  13. graphicsView->setScene(scene);
  14. graphicsView->show();
  15. }
To copy to clipboard, switch view to plain text mode 

and...

Qt Code:
  1. //rectMap.cpp
  2. #include "rectMap.h"
  3. #include <QGraphicsScene>
  4. #include <QPainter>
  5. #include <QStyleOption>
  6. #include <math.h>
  7. #include <QGraphicsSceneHoverEvent>
  8. #include <QStyleOptionGraphicsItem>
  9.  
  10. RectMap::RectMap(int Size, QGraphicsItem *parent)
  11. :color(qrand() % 256, qrand() % 256, qrand() % 256), QGraphicsItem(parent), m_isHovered(false), m_mouseIsDown(false)
  12. {
  13. size = Size;
  14. setFlag(QGraphicsItem::ItemIsSelectable);
  15. setFlag(QGraphicsItem::ItemIsMovable);
  16. setAcceptHoverEvents(true);
  17. }
  18.  
  19. void RectMap::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
  20. {
  21. m_isHovered = true;
  22. QGraphicsItem::hoverEnterEvent(e);
  23. }
  24.  
  25. void RectMap::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
  26. {
  27. m_isHovered = false;
  28. QGraphicsItem::hoverLeaveEvent(e);
  29. }
  30.  
  31. void RectMap::mousePressEvent(QGraphicsSceneMouseEvent *e)
  32. {
  33. m_mouseIsDown = true;
  34. QGraphicsItem::mousePressEvent(e);
  35. update();
  36. }
  37.  
  38. void RectMap::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
  39. {
  40. m_mouseIsDown = false;
  41. QGraphicsItem::mouseReleaseEvent(e);
  42. update();
  43. }
  44. QRectF RectMap::boundingRect() const
  45. {
  46. qreal adjust = 0.5;
  47. return QRectF( 0 - adjust, -262 - adjust, size + adjust, 523 + adjust);
  48. }
  49.  
  50. QPainterPath RectMap::shape() const
  51. {
  52. path.addRect(-100, -200, 20, 40);
  53. return path;
  54. }
  55.  
  56.  
  57. void RectMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  58. {
  59. QPen pen;
  60. bool isSelected = (option->state & QStyle::State_Selected) == QStyle::State_Selected;
  61. pen.setColor(isSelected ? Qt::green : Qt::blue);
  62. pen.setWidth(1);
  63. pen.setStyle(m_mouseIsDown ? Qt::DashDotLine : Qt::DotLine);
  64. painter->setPen(pen);
  65. painter->setBrush(m_isHovered ? Qt::red : Qt::yellow);
  66. painter->drawRect(0,-262,size,523);
  67. }
To copy to clipboard, switch view to plain text mode