Change how selected a QGraphicsItem object is displayed
-
Hi,
I have a class
JoinItem
derived fromQGraphicsRectItem
. When selected such an item should be painted blue and otherwise black. Instead of the default where it gets surrounded by a dashed rectangle. So I just want to change how a selectedQGraphicsRectItem
is displayed (everything else should be as before).My implementation works but I wonder whether it is the "right thing" ...
In
JoinItem
thepaint
method is implemented as follows:void JoinItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QStyleOptionGraphicsItem opt = *option; if (isSelected()) { setPen(QPen(Qt::blue, 2)); opt.state = QStyle::State_None; } else { setPen(QPen(Qt::black, 2)); } QGraphicsRectItem::paint(painter, &opt, widget); }
So my hack is that
QGraphicsRectItem::paint
simply never gets the stateQStyle::State_Selected
. Making a copy ofoption
in ant case seems to be a crude hack. -
@Michael-Lehn said in Change how selected a QGraphicsItem object is displayed:
My implementation works but I wonder whether it is the "right thing" ...
Should be fine since you only change the style. The selection behavior should not be affected
-