how do I make mousedoubleclick(event) to work on my current QGraphicsRectItems?
First, remove the inheritance from QGraphicsRectItem that you added to GatewayWidget, and remove the mouseDoubleClickEvent() code too.
Second, the steps you need to follow are these:
1. Derive a new class from QGraphicsRectItem and add an event handler for the mouseDoubleClickEvent to it:
// .h
{
public:
protected:
// If you want to handle other events, add handlers for those here too
}
// .cpp
// Implement the event handler
{
// You now have the double-click event. The rect that was clicked is "this". What do you want to do?
// If you want to also have the QGraphicsRectItem base class process this event, you must call
}
// .h
class GatewayRectItem : public QGraphicsRectItem
{
public:
GatewayRectItem( QGraphicsItem * parent = nullptr ) : QGraphicsRectItem( parent ) {}
protected:
void mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event );
// If you want to handle other events, add handlers for those here too
}
// .cpp
// Implement the event handler
void GatewayRectItem::mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event )
{
// You now have the double-click event. The rect that was clicked is "this". What do you want to do?
// If you want to also have the QGraphicsRectItem base class process this event, you must call
QGraphicsRectItem::mouseDoubleClickEvent( event );
}
To copy to clipboard, switch view to plain text mode
2 - When you create your rect items, you call the new constructor. You also need to make sure the item is selectable so it responds to mouse clicks:
GatewayRectItem * rect = new GatewayRectItem();
scene->addItem( rect );
GatewayRectItem * rect = new GatewayRectItem();
rect->setFlags( QGraphicsItem::ItemIsSelectable );
scene->addItem( rect );
To copy to clipboard, switch view to plain text mode
3 - Decide what you want to do with the double-click. In this case, I would have the GatewayRectItem emit a signal that you can handle somewhere else in a slot that can determine the correct color. To do that, you need to also derive GatewayRectItem from QObject so it can send signals:
// .h
{
Q_OBJECT;
public:
signals:
protected:
}
// .cpp
{
// Can just do this here, so all items have the flag set
}
// Implement the event handler
{
// You now have the double-click event. Emit the signal to let a slot know
emit doubleClicked( event );
// If you want to also have the QGraphicsRectItem base class process this event, you must call
}
// .h
class GatewayRectItem : public QObject, public QGraphicsRectItem
{
Q_OBJECT;
public:
GatewayRectItem( QGraphicsItem * parent = nullptr );
signals:
void doubleClicked( QGraphicsSceneMouseEvent * event );
protected:
void mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event );
}
// .cpp
GatewayRectItem::GatewayRectItem( QGraphicsItem * parent )
: QGraphicsRectItem( parent )
{
// Can just do this here, so all items have the flag set
setFlags( QGraphicsItem::ItemIsSelectable );
}
// Implement the event handler
void GatewayRectItem::mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event )
{
// You now have the double-click event. Emit the signal to let a slot know
emit doubleClicked( event );
// If you want to also have the QGraphicsRectItem base class process this event, you must call
QGraphicsRectItem::mouseDoubleClickEvent( event );
}
To copy to clipboard, switch view to plain text mode
4 - Add a slot to handle this signal. I assume you would want to do that in your GatewayWidget class:
// .h
class GatewayWidget : public VgdWidget
{
// ... all the original code
public slots:
// ...
};
// .cpp
{
// first, get the rect instance that sent the signal
GatewayRectItem * rect = qobject_cast< GatewayRectItem * >( sender() );
// Now you have the rect that was double-clicked, and you have the event details if you need them
// Do whatever you need to do to determine what color the rect should be, then set the color
rect
->setBrush
( QBrush( theNewColor
) );
}
// .h
class GatewayWidget : public VgdWidget
{
// ... all the original code
public slots:
void onRectDoubleClicked( QGraphicsSceneMouseEvent * event );
// ...
};
// .cpp
void GatewayWidget::onRectDoubleClicked( QGraphicsSceneMouseEvent * event )
{
// first, get the rect instance that sent the signal
GatewayRectItem * rect = qobject_cast< GatewayRectItem * >( sender() );
// Now you have the rect that was double-clicked, and you have the event details if you need them
// Do whatever you need to do to determine what color the rect should be, then set the color
rect->setBrush( QBrush( theNewColor ) );
}
To copy to clipboard, switch view to plain text mode
5 - And finally, when you create the rect, you need to connect its signal to the GatewayWidget's slot:
GatewayRectItem * rect = new GatewayRectItem();
// Don't need this now since it is set in the constructor for GatewayRectItem
// rect->setFlags( QGraphicsItem::ItemIsSelectable );
scene->addItem( rect );
connect( rect, &GatewayRectItem::doubleClicked, this, &GatewayWidget::onRectDoubleClicked );
GatewayRectItem * rect = new GatewayRectItem();
// Don't need this now since it is set in the constructor for GatewayRectItem
// rect->setFlags( QGraphicsItem::ItemIsSelectable );
scene->addItem( rect );
connect( rect, &GatewayRectItem::doubleClicked, this, &GatewayWidget::onRectDoubleClicked );
To copy to clipboard, switch view to plain text mode
None of this code has been tested - I am just writing it here. There could be typos, so don't just copy and paste without checking it.
Bookmarks