Here is my *.cpp file contents

Qt Code:
  1. #include "blocksegmentitem.h"
  2.  
  3. BlockSegmentItem::BlockSegmentItem(QString id,int _x1,int _y1, int _x2,int _y2
  4. ,FieldItemLabel _label,QString _signalization):FieldItem(id,FieldProperty::BLOCKSEGMENTITEM)
  5. {
  6. x1 = _x1;
  7. y1 = _y1;
  8. x2 = _x2;
  9. y2 = _y2;
  10. label = _label;
  11. signalization = _signalization;
  12. createActions();
  13. createContextMenu();
  14. }
  15.  
  16. BlockSegmentItem::~BlockSegmentItem()
  17. {
  18.  
  19. }
  20.  
  21. QRectF BlockSegmentItem::boundingRect() const
  22. {
  23. int rectLeft = x1 < x2 ? x1 : x2;
  24. int rectTop = y1 < y2 ? y1 : y2;
  25. return QRectF(QPointF(rectLeft,rectTop),sizeHint(Qt::PreferredSize,QSizeF(-1,-1))).adjusted(-2,-2,0,0);
  26. }
  27.  
  28. void BlockSegmentItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  29. {
  30. Q_UNUSED(option);
  31. Q_UNUSED(widget);
  32.  
  33. painter->setRenderHint(QPainter::Antialiasing);
  34.  
  35. if(signalization == "NO")
  36. {
  37. //Custom pen with width = 3
  38. painter->setPen(dashedLinePen);
  39. painter->drawLine(x1,y1,x2,y2);
  40. }
  41. else
  42. {
  43. //Custom pen with width = 4
  44. painter->setPen(solidLinePen);
  45. painter->drawLine(x1,y1,x2,y2);
  46. }
  47.  
  48. if(label.visible)
  49. {
  50. if(label.rotateAngle != 0)
  51. {
  52. painter->drawText(label.x,label.y,label.text);
  53. }
  54. else
  55. {
  56. painter->drawText(label.x,label.y,label.text);
  57. }
  58. }
  59. }
  60.  
  61.  
  62. QSizeF BlockSegmentItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
  63. {
  64. Q_UNUSED(which);
  65. Q_UNUSED(constraint);
  66. return QSizeF(abs(x2-x1), abs(y2-y1) > 0 ? abs(y2-y1) : 4);
  67. }
  68.  
  69. QPointF BlockSegmentItem::pos() const
  70. {
  71. return QPointF(x1,y1);
  72. }
  73.  
  74. void BlockSegmentItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  75. {
  76. contextMenu->exec(event->screenPos());
  77. }
  78.  
  79. QPainterPath BlockSegmentItem::shape() const
  80. {
  81. path.moveTo(x1,y1);
  82. path.lineTo(x2,y2);
  83. return path;
  84. }
  85.  
  86. void BlockSegmentItem::changeLabelVisibility(bool visible)
  87. {
  88. label.visible = visible;
  89. }
  90.  
  91. void BlockSegmentItem::createContextMenu()
  92. {
  93. contextMenu = new QMenu("BlockContextMenu");
  94. contextMenu->addAction(blockProtectionAction);
  95. }
  96.  
  97. void BlockSegmentItem::createActions()
  98. {
  99. blockProtectionAction = new QAction("Blogu korumaya al",this);
  100. connect(blockProtectionAction,SIGNAL(triggered()),this,SLOT(blockProtectionRequested()));
  101. }
  102.  
  103. void BlockSegmentItem::blockProtectionRequested()
  104. {
  105. qDebug()<<"Block Protection requested on: "<<getId();
  106. }
To copy to clipboard, switch view to plain text mode 

I think it could be related to pen because I am painting a line in paint method. Naturally pen is very thin element and context menu event only occurs when I click a point on line.
Am I right? If I am right how can I do a better hit test.