GraphicsView mousePressEvent problem
Hello, i have a graphicsview and i implement QGraphicsItem::mousePressEvent function, so when i click on left button i can see the mouse position.
The problem is: in some places of graphicsview, the event mousePressEvent is ignored - most specifically in corner areas - so the event works fine except near the corners.
Can anyone help me??
Thanks,
Sérgio Silva
Re: GraphicsView mousePressEvent problem
Maybe post your implementation, it's rather hard to guess what could cause it.
Are you sure that you don't have anything on the scene that could catch and accept the event before it gets to your class? Or maybe some kind of event filter ?
Re: GraphicsView mousePressEvent problem
I think that i'm doing everything correct. I have a mainwindow class with a graphicsview, and in constructor of mainwindow i do:
ui->graphicsView->setInteractive(true);
ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );
ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysO ff);
scene = new QGraphicsScene(-2,-2,904,636);
ui->graphicsView->setScene(scene);
campo = new CCampo(904, 636);
scene->addItem(campo);
CCampo class is: .h:
class CCampo: public QObject , public QGraphicsItem
{
Q_OBJECT
public:
CCampo(int resX,int resY);
~CCampo();
private:
int x, y;
QRectF boundingRect() const;
QPainterPath shape() const;
QPainter *Cpainter;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};
.cpp:
CCampo::CCampo(int resX,int resY)
{
setAcceptsHoverEvents(true);
x = resX;
y = resY;
setPos(0, 0);
Cpainter = new QPainter;
}
CCampo::~CCampo()
{
}
QRectF CCampo::boundingRect() const
{
return QRectF(0,0, x, y);
}
QPainterPath CCampo::shape() const
{
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}
void CCampo::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
Q_UNUSED(option);
painter->setPen(QPen(Qt::black, 4, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
painter->setRenderHint(QPainter::Antialiasing,true);
painter->setBrush(QBrush(Qt::green,Qt::SolidPattern));
painter->drawRect(( QRectF(0, 0,900,630)));
}
void CCampo::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug("Mouse event");
if(event->button() == Qt::LeftButton)
{
qDebug("X: %d, Y: %d", event->pos().x(), event->pos().y());
}
QGraphicsItem::mousePressEvent(event);
}
what i don't understand is that it works in some zones but don't work in anothers :p:p:p.
Thanks,
Sérgio Silva
Re: GraphicsView mousePressEvent problem
Quote:
what i don't understand is that it works in some zones but don't work in anothers
As you've posted, it wont work at the corners ?
Maybe that's why :
Quote:
Code:
{
path.addEllipse(boundingRect());
return path;
}
QGraphicsItem::shape() is used for collision detection, hit tests ect.
You are returning an elliptic shape, which fits into item's rect. This ellipse does not covers the entire rectangle.
Try returning rectangular shape:
Code:
path.addRect(boundingRect());
return path;
Re: GraphicsView mousePressEvent problem
Hey :):):), works now!!
It's the line - path.addRect(boundingRect());
stampede, thanks for help me :) .
Sérgio Silva