Problem with the Mouse Events and Hover Events of QGraphicsRectItem.
-
I created a sub-class of QGraphicsView. On View, i draw an image. I want a rect around an image when i click on it. For this, I created a sub-class of QGraphicsRectItem and QObject. I set the images for size grip on appropriate locations. Rect around the image is showing. But, When i click on it or hover the mouse on it, its mouse events and hover events is not working.
The following code is of sub-class of QGraphicsRectItem and QObject.
@class WhiteBoardItemRect : public QGraphicsRectItem , public QObject
{
public:
WhiteBoardItemRect(qreal xPos, qreal yPos, int width, int height);virtual ~WhiteBoardItemRect(); QGraphicsSvgItem* closeBtn; QGraphicsSvgItem* resizeBtn; QGraphicsSvgItem* topResizeBtn; QGraphicsSvgItem* bottomResizeBtn; QGraphicsSvgItem* leftResizeBtn; QGraphicsSvgItem* rightResizeBtn;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
signals:
public slots:
private:
qreal xCor, yCor; int rectWidth, rectHeight;
};@
@WhiteBoardItemRect::WhiteBoardItemRect(qreal xPos, qreal yPos, int width, int height) :
QGraphicsRectItem(),
QObject(),
xCor(xPos),
yCor(yPos),
rectWidth(width),
rectHeight(height)
{
setAcceptedMouseButtons(Qt::LeftButton);setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); this->setAcceptHoverEvents(true); this->setAcceptDrops(true); QPen penForRect; penForRect.setWidth(15); penForRect.setColor(QColor(255,204,204)); this->setPen(penForRect); this->setRect(QRect(xCor, yCor, rectWidth, rectHeight)); this->show(); closeBtn = new QGraphicsSvgItem(":/l/imageicon/close.svg", this); closeBtn->setParentItem(this); QRectF closeRect = closeBtn->mapRectToParent(closeBtn->boundingRect()); closeBtn->setPos((rect().left() - closeRect.width()) +5 , (rect().top() - closeRect.height()) + 5); resizeBtn = new QGraphicsSvgItem(":/l/imageicon/resize.svg", this); resizeBtn->setParentItem(this); QRectF resizeRect = resizeBtn->mapRectToParent(resizeBtn->boundingRect()); resizeBtn->setPos((rect().right() - resizeRect.width()) +5 , (rect().bottom() - resizeRect.height()) + 5); topResizeBtn = new QGraphicsSvgItem(":/l/imageicon/resizeTop.svg", this); topResizeBtn->setParentItem(this); QRectF topRect = topResizeBtn->mapRectToParent(topResizeBtn->boundingRect()); topResizeBtn->setPos(rect().center().x() - topRect.width() / 2, rect().y() - 5); bottomResizeBtn = new QGraphicsSvgItem(":/l/imageicon/resizeBottom.svg", this); bottomResizeBtn->setParentItem(this); QRectF bottomRect = bottomResizeBtn->mapRectToParent(bottomResizeBtn->boundingRect()); bottomResizeBtn->setPos(rect().center().x() - bottomRect.width() / 2, rect().bottom() - bottomRect.height() + 5); leftResizeBtn = new QGraphicsSvgItem(":/l/imageicon/resizeLeft.svg", this); leftResizeBtn->setParentItem(this); QRectF leftRect = leftResizeBtn->mapRectToParent(leftResizeBtn->boundingRect()); leftResizeBtn->setPos(rect().left() - 5, rect().center().y() - leftRect.height() / 2); rightResizeBtn = new QGraphicsSvgItem(":/l/imageicon/resizeRight.svg", this); rightResizeBtn->setParentItem(this); QRectF rightRect = rightResizeBtn->mapRectToParent(rightResizeBtn->boundingRect()); rightResizeBtn->setPos(rect().right() - rightRect.width() + 5, rect().center().y() - rightRect.height() / 2);
}
WhiteBoardItemRect::~WhiteBoardItemRect()
{}
void WhiteBoardItemRect::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "In mouse press of REct item";
}void WhiteBoardItemRect::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "In mouse move of REct item";
}void WhiteBoardItemRect::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "In mouse release of REct item";
}void WhiteBoardItemRect::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
qDebug() << "In hoverEnterEvent of REct item";}
void WhiteBoardItemRect::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
qDebug() << "In hoverLeaveEvent of REct item";}
void WhiteBoardItemRect::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
qDebug() << "In hoverMoveEvent of REct item";}
@
The Following code is creating Rect around image on the mouseRelease event of QGraphicsView.
@ rectForSelectedItem = new WhiteBoardItemRect(topLeftCornrOfSelectedItem.x(), topLeftCornrOfSelectedItem.y()
, outerRectOfItem.width(), outerRectOfItem.height());this->scene()->addItem(rectForSelectedItem); vbIsRectSelected = true; QGraphicsItem* itemUnderMouse = this->itemAt(event->pos()); if(!itemUnderMouse) delete rectForSelectedItem;@
Thanks in advance..... :)
-
If you derive from QObject, you should add the Q_OBJECT macro in the private section of the class. This is not the reason why it doesn't work, though.
Did you solve the issue in the meanwhile. If not, please provide a full minimum working example.
-
Thnx for the reply. No, i did not solve my issue till now.
I am sharing my project's minimum code. You can download code from the following link."Download zip file from here for my project":https://skydrive.live.com/redir?resid=D74B283B4D849C6A!125&authkey=!AFkNTIHRNv5HqPY
-
The WhiteBoardItemRect is never created, so the virtual methods are never called. To me it looks like you have to fix your application logic first.