Change how selected a QGraphicsItem object is displayed
-
Hi,
I have a class
JoinItemderived 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 selectedQGraphicsRectItemis displayed (everything else should be as before).My implementation works but I wonder whether it is the "right thing" ...
In
JoinItemthepaintmethod 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::paintsimply never gets the stateQStyle::State_Selected. Making a copy ofoptionin ant case seems to be a crude hack. -
Hi,
I have a class
JoinItemderived 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 selectedQGraphicsRectItemis displayed (everything else should be as before).My implementation works but I wonder whether it is the "right thing" ...
In
JoinItemthepaintmethod 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::paintsimply never gets the stateQStyle::State_Selected. Making a copy ofoptionin 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
-
M Michael Lehn has marked this topic as solved on