Results 1 to 5 of 5

Thread: Context Menu event with QGraphicsWidget

  1. #1
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Context Menu event with QGraphicsWidget

    In my application I subclass QGraphicsWidget
    In paint I am drawing a line with pen width 4.
    I reimplemented boundingRect() and shape().
    But I can't catch context menu event every time I click right mouse button.
    What is the problem.(Pen Width ? )

    Qt Code:
    1. //Sample code for boundingRect() and shape()
    2.  
    3. QRectF boundingRect() const
    4. {
    5. qreal rectLeft = x1 < x2 ? x1 : x2;
    6. qreal rectTop = y1 < y2 ? y1 : y2;
    7. qreal rectWidth = (x1 - x2) != 0 ? abs(x1-x2) : 4;
    8. qreal rectHeight = (y1 - y2) != 0 ? abs(y1 -y2) : 4;
    9.  
    10. return QRectF(rectLeft,rectTop,rectWidth,rectHeigt);
    11. }
    12.  
    13. QPainterPath shape()
    14. {
    15. path.addRect(boundingRect());
    16. return path;
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by onurozcelik; 10th June 2010 at 10:13. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Context Menu event with QGraphicsWidget

    But I can't catch context menu event every time I click right mouse button.
    What is the problem.
    you have to implement QGraphicsItem::contextMenuEvent()
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Context Menu event with QGraphicsWidget

    I have already reimplemented QGraphicsItem::contextMenuEvent()
    But sometimes event works sometimes does not.
    Could it be related with pen?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Context Menu event with QGraphicsWidget

    I don't think so, why do you think it could be related to the pen?
    Can you show your implementation of contectMenuEvent()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Context Menu event with QGraphicsWidget

    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.

Similar Threads

  1. Context Menu & QGraphicsWidget
    By onurozcelik in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2010, 12:08
  2. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 22:02
  3. Separator in context menu
    By schall_l in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2009, 17:09
  4. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 08:55
  5. Context Menu
    By RY in forum Newbie
    Replies: 1
    Last Post: 10th September 2008, 07:59

Tags for this Thread

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.