Modify QPainterPath object
-
Hi,
I have a class "Node" which is inherited from QGraphicsItem class, and there is a method called shape where I wanna use ellipse to draw node.QPainterPath Cvor::shape() const { QPainterPath temp; temp.addEllipse(-10, -10, 30, 30); return temp; }
I want to make this ellipse bigger, but I can't make it. Size is very small and I think it's default. Is there any way to resize it?
Thanks in advance -
Hi
Make sure your
https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
returns a rect big enough for the ellipse you want.
Also note the info about calling prepareGeometryChange()
as else you get odd redrawing. -
Hi,
What about drawing an ellipse that takes into account the size of your item ?
-
- How are you overriding these functions:
Boundingrect, Shape, Paint
?
Boundingrect - notifies the view about item area (necessary for redraw) and make your item supports click events.
Shape - is used for more accurate collision detection or Clip Area (whenQGraphicsItem::ItemClipsToShape
flag is activated)- Are you trying to redefine the node size at run time? If yes, have you tried to store a QRect item or a Size Variable? Because you could redefine the size and call prepareGeometryChanges ().
YourItem::YourItem() { _size = 10; // radius } QRectF YourItem::boundingRect() const { return QRectF(-_size/2, -_size/2, _size, _size); } void YourItem::setSize(qreal size) { _size = size; prepareGeometryChange(); }
- How are you overriding these functions:
-
@KillerSmath I tried, but still the same
-
@Annabela_Cortez
Did you read my first question ?How are you overriding these functions: Boundingrect, Shape, Paint ?
We cannot help you without a knowledge of how you are implementing your solution.
-
QRectF Cvor::boundingRect() const { qreal adjust = 2; return QRectF(-10 - adjust, -10 - adjust,23 + adjust, 23 + adjust); } QPainterPath Cvor::shape() const { QPainterPath temp; temp.addEllipse(-10, -10, 30, 30); return temp; } void Cvor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { painter->setPen(Qt::NoPen); painter->setBrush(Qt::darkGray); painter->drawEllipse(-7, -7, 20, 20); QRadialGradient gradient(-3, -3, 10); if (option->state & QStyle::State_Sunken) { gradient.setCenter(3, 3); gradient.setFocalPoint(3, 3); gradient.setColorAt(1, QColor(Qt::black).light(120)); gradient.setColorAt(0, QColor(Qt::darkRed).light(120)); } else { gradient.setColorAt(0, Qt::black); gradient.setColorAt(1, Qt::darkRed); } painter->setBrush(gradient); painter->setPen(QPen(Qt::black, 0)); painter->drawEllipse(-10, -10, 20, 20); }
-
@Annabela_Cortez
Shape has not directly effect on way which your item is painted. The only exception is whenQGraphicsItem::ItemClipsToShape
flag is activated like i mentioned in the above response.If your goal is change the size of this node then say to the code Paint a bigger node instead of change the Shape value. (Replace
YOUR_SIZE
to your prefered size).void Cvor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { painter->setPen(Qt::NoPen); painter->setBrush(Qt::darkGray); painter->drawEllipse(-YOUR_SIZE/2 - 3, -YOUR_SIZE/2 - 3, YOUR_SIZE, YOUR_SIZE); QRadialGradient gradient(-3, -3, 10); if (option->state & QStyle::State_Sunken) { gradient.setCenter(3, 3); gradient.setFocalPoint(3, 3); gradient.setColorAt(1, QColor(Qt::black).light(120)); gradient.setColorAt(0, QColor(Qt::darkRed).light(120)); } else { gradient.setColorAt(0, Qt::black); gradient.setColorAt(1, Qt::darkRed); } painter->setBrush(gradient); painter->setPen(QPen(Qt::black, 0)); painter->drawEllipse(-YOUR_SIZE/2, -YOUR_SIZE/2, YOUR_SIZE, YOUR_SIZE); }