Hello erverybody, have some problems with the mousePressevent in combination with QGraphicsSceneMouseEvent.
Everything works fine, but the mouse events work with the bounding Rect but not exclusively only the Qpolygon inside of the bounding rect.
Since i do have to rotate the Qpolygon, and other items are very close to each other (also want to click them seperatly) this is not the best practise for me...Any help is appreciated.
Since i will position the Qpolygon from its center, i wrote a small conversion funciton, (early stage, will program it out late
frame.cpp
Qt Code:
  1. #include "frame.h"
  2.  
  3. frame::frame(QGraphicsItem *parent)
  4. : umbrella(parent)
  5. {
  6. this->frameRotation=0;
  7. }
  8.  
  9. QRectF frame::boundingRect() const
  10. {
  11. return QRectF(0,0,128,128);
  12. }
  13.  
  14. void frame::paint(QPainter *painter,
  15. const QStyleOptionGraphicsItem *option, QWidget *widget)
  16. {
  17. Q_UNUSED(option);
  18. Q_UNUSED(widget);
  19. QPolygon myframePolygon;
  20. QColor myframecolor;
  21. QBrush myframeBrush;
  22. QPainterPath myframePainterPath;
  23. QPen myframePen;
  24. painter->drawRect(boundingRect());
  25. myframePen.setWidth(4);
  26. myframePen.setColor(Qt::black);
  27. myframePen.setJoinStyle(Qt::RoundJoin);
  28. myframecolor=(QColor(0,0,255,70));
  29. myframeBrush.setColor(myframecolor);
  30. myframeBrush.setStyle(Qt::SolidPattern);
  31. myframePolygon.append(QPoint(2,27));
  32. myframePolygon.append(QPoint(30,1));
  33. myframePolygon.append(QPoint(65,8));
  34. myframePolygon.append(QPoint(100,1));
  35. myframePolygon.append(QPoint(127,28));
  36. myframePolygon.append(QPoint(119,64));
  37. myframePolygon.append(QPoint(122,96));
  38. myframePolygon.append(QPoint(99,126));
  39. myframePolygon.append(QPoint(64,119));
  40. myframePolygon.append(QPoint(29,125));
  41. myframePolygon.append(QPoint(5,96));
  42. myframePolygon.append(QPoint(9,62));
  43. myframePainterPath.addPolygon(myframePolygon);
  44. int frameWide =128;
  45. int frameHigh =128;
  46. int xc = 0 + frameWide/2;
  47. int yc = 0 + frameHigh/2;
  48. painter->translate(+xc,+yc);
  49. painter->rotate(frameRotation);
  50. painter->translate(-xc,-yc);
  51. painter->setPen(myframePen);
  52. painter->drawPolygon(myframePolygon);
  53. painter->fillPath(myframePainterPath,myframeBrush);
  54. }
  55.  
  56. void frame::mousePressEvent(QGraphicsSceneMouseEvent *event){
  57. if(event->button() == Qt::RightButton){
  58. qDebug() << "click with the right mouse button";
  59. }
  60. else if(event->button() == Qt::LeftButton){
  61. qDebug() << "click with the left mouse button";
  62. }
  63. else{
  64. qDebug() << "click with the whell button";
  65. }
  66. QGraphicsItem::mousePressEvent(event);
  67. }
  68.  
  69. void frame::setFramePosMiddle(qreal x, qreal y)
  70. {
  71. this->setPos(x-128/2,y-128/2);
  72. }
  73.  
  74. void frame::setFrameRotation(qreal frameRotation)
  75. {
  76. this->frameRotation=frameRotation;
  77. update();
  78. }
To copy to clipboard, switch view to plain text mode 

frame.h
Qt Code:
  1. #ifndef FRAME_H
  2. #define FRAME_H
  3.  
  4. #include <QGraphicsItem>
  5. #include <QtGui>
  6. #include "umbrella.h"
  7.  
  8. class umbrella;
  9. class QParallelAnimationGroup;
  10.  
  11. class frame : public umbrella
  12. {
  13. public:
  14. frame(QGraphicsItem *parent = 0);
  15. QRectF boundingRect() const;
  16. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
  17. void setFramePosMiddle(qreal x,qreal y);
  18. void setFrameRotation(qreal frameRotation);
  19. protected:
  20. void mousePressEvent(QGraphicsSceneMouseEvent *event);
  21. qreal x,y,frameRotation;
  22. };
  23.  
  24. #endif // FRAME_H
To copy to clipboard, switch view to plain text mode 

Result
Screenshot.png

All mouse events work, but i want that only the events work in the Qpolygon not alos in the show boundingRect()