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 ? )
Code:
//Sample code for boundingRect() and shape()
{
qreal rectLeft = x1 < x2 ? x1 : x2;
qreal rectTop = y1 < y2 ? y1 : y2;
qreal rectWidth = (x1 - x2) != 0 ? abs(x1-x2) : 4;
qreal rectHeight = (y1 - y2) != 0 ? abs(y1 -y2) : 4;
return QRectF(rectLeft,rectTop,rectWidth,rectHeigt
);
}
{
path.addRect(boundingRect());
return path;
}
Re: Context Menu event with QGraphicsWidget
Quote:
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()
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?
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()?
Re: Context Menu event with QGraphicsWidget
Here is my *.cpp file contents
Code:
#include "blocksegmentitem.h"
BlockSegmentItem
::BlockSegmentItem(QString id,
int _x1,
int _y1,
int _x2,
int _y2
,FieldItemLabel _label,
QString _signalization
):FieldItem
(id,FieldProperty
::BLOCKSEGMENTITEM){
x1 = _x1;
y1 = _y1;
x2 = _x2;
y2 = _y2;
label = _label;
signalization = _signalization;
createActions();
createContextMenu();
}
BlockSegmentItem::~BlockSegmentItem()
{
}
QRectF BlockSegmentItem
::boundingRect() const {
int rectLeft = x1 < x2 ? x1 : x2;
int rectTop = y1 < y2 ? y1 : y2;
return QRectF(QPointF(rectLeft,rectTop
),sizeHint
(Qt
::PreferredSize,
QSizeF(-1,
-1))).
adjusted(-2,
-2,
0,
0);
}
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter
->setRenderHint
(QPainter::Antialiasing);
if(signalization == "NO")
{
//Custom pen with width = 3
painter->setPen(dashedLinePen);
painter->drawLine(x1,y1,x2,y2);
}
else
{
//Custom pen with width = 4
painter->setPen(solidLinePen);
painter->drawLine(x1,y1,x2,y2);
}
if(label.visible)
{
if(label.rotateAngle != 0)
{
painter->drawText(label.x,label.y,label.text);
}
else
{
painter->drawText(label.x,label.y,label.text);
}
}
}
QSizeF BlockSegmentItem
::sizeHint(Qt
::SizeHint which,
const QSizeF &constraint
) const {
Q_UNUSED(which);
Q_UNUSED(constraint);
return QSizeF(abs(x2
-x1
),
abs(y2
-y1
) >
0 ?
abs(y2
-y1
) : 4);
}
QPointF BlockSegmentItem
::pos() const {
}
{
contextMenu->exec(event->screenPos());
}
{
path.moveTo(x1,y1);
path.lineTo(x2,y2);
return path;
}
void BlockSegmentItem::changeLabelVisibility(bool visible)
{
label.visible = visible;
}
void BlockSegmentItem::createContextMenu()
{
contextMenu
= new QMenu("BlockContextMenu");
contextMenu->addAction(blockProtectionAction);
}
void BlockSegmentItem::createActions()
{
blockProtectionAction
= new QAction("Blogu korumaya al",
this);
connect(blockProtectionAction,SIGNAL(triggered()),this,SLOT(blockProtectionRequested()));
}
void BlockSegmentItem::blockProtectionRequested()
{
qDebug()<<"Block Protection requested on: "<<getId();
}
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.