How to draw rectangle with all corners rounded ?
-
@tushu add print message or set break point there to check if the func is called. Also show your full code of this func.
-
@JoeCFD
Some big rectangle is getting drawn.
I think that is because ofQRectF rect = QRectF(widget->rect());
I am taking widget->rect().
But I am expecting, every rectangle in the scene should have rounded corner.Something like this.
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QRectF rect = QRectF(widget->rect()); QPainterPath path; path->drawRoundedRect(rect, 10, 10); /* path not painter */ auto copied_option = *option; copied_option.state &= ~QStyle::State_Selected; auto selected = option->state & QStyle::State_Selected; //QGraphicsRectItem::paint(painter, &copied_option, widget); /* you do not call this one */ painter->save(); if (selected) { painter->setBrush(Qt::NoBrush); painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine)); } else { set pen color; } painter->drawPath(path); painter->restore(); }
-
Something like this.
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QRectF rect = QRectF(widget->rect()); QPainterPath path; path->drawRoundedRect(rect, 10, 10); /* path not painter */ auto copied_option = *option; copied_option.state &= ~QStyle::State_Selected; auto selected = option->state & QStyle::State_Selected; //QGraphicsRectItem::paint(painter, &copied_option, widget); /* you do not call this one */ painter->save(); if (selected) { painter->setBrush(Qt::NoBrush); painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine)); } else { set pen color; } painter->drawPath(path); painter->restore(); }
I have used
painter.drawRoundedRect(rect, 10, 10);
because QPainterPath does not have drawRoundedRect()
current output
Below lines creating difference.
//QGraphicsRectItem::paint(painter, &copied_option, widget); /* you do not call this one */
-
I have used
painter.drawRoundedRect(rect, 10, 10);
because QPainterPath does not have drawRoundedRect()
current output
Below lines creating difference.
//QGraphicsRectItem::paint(painter, &copied_option, widget); /* you do not call this one */
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QRectF rect = QRectF(widget->rect()); QPainterPath path; path->addRoundedRect(rect, 10, 10); /* add not draw sorry */ auto copied_option = *option; copied_option.state &= ~QStyle::State_Selected; auto selected = option->state & QStyle::State_Selected; //QGraphicsRectItem::paint(painter, &copied_option, widget); /* drawing is finished here and no need to call parent->paint() */ painter->save(); if (selected) { painter->setBrush(Qt::NoBrush); painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine)); } else { set pen color; } painter->drawPath(path); painter->restore(); }
-
I have used
painter.drawRoundedRect(rect, 10, 10);
because QPainterPath does not have drawRoundedRect()
current output
Below lines creating difference.
//QGraphicsRectItem::paint(painter, &copied_option, widget); /* you do not call this one */
@tushu You also need to try the example first
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->setRenderHint(QPainter::Antialiasing); QPainterPath path; path.addRoundedRect(QRectF(10, 10, 100, 50), 10, 10); QPen pen(Qt::black, 10); painter->setPen(pen); painter->fillPath(path, Qt::red); painter->drawPath(path); }
-
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QRectF rect = QRectF(widget->rect()); QPainterPath path; path->addRoundedRect(rect, 10, 10); /* add not draw sorry */ auto copied_option = *option; copied_option.state &= ~QStyle::State_Selected; auto selected = option->state & QStyle::State_Selected; //QGraphicsRectItem::paint(painter, &copied_option, widget); /* drawing is finished here and no need to call parent->paint() */ painter->save(); if (selected) { painter->setBrush(Qt::NoBrush); painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine)); } else { set pen color; } painter->drawPath(path); painter->restore(); }
-
@JoeCFD
Is the below line correct ?QRectF rect = QRectF(widget->rect());
because of the above line , it is creating bigger rectangle ?
But bigger rectangle ( outermost ) has curved corner. -
@JoeCFD
Your rectangle size suggestion worked.
Current output.Now I want to round corner of middle 5 rectangle only ( i.e. sw1,sw2,PD_blue, PD_green,PD_yellow)
But left side rectangle ( small ) also got changed and looking like circle.I think, I will need to set some flag and will have to decide, which rectangle's corner should be rounded.
But now main concern is PD_blue, PD_green,PD_yellow rectangles color is not visible.
I set their color, after creating rectangle using setBrush(). I do not set their color in paint(). After creating rectangle, if they are full filling some criteria, then I set color on them.
Why that color is not visible ?
Here is my paint()
void guiSchematicRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); QRectF rect = QRectF(_polyRect); QPainterPath path; path.addRoundedRect(rect, 4, 4); auto copied_option = *option; copied_option.state &= ~QStyle::State_Selected; auto selected = option->state & QStyle::State_Selected; painter->save(); if (selected) { painter->setBrush(Qt::NoBrush); painter->setPen(QPen(option->palette.windowText(), 1, Qt::SolidLine)); } else { painter->setPen(Qt::blue); } painter->drawPath(path); painter->restore(); }
-
@JoeCFD
Your rectangle size suggestion worked.
Current output.Now I want to round corner of middle 5 rectangle only ( i.e. sw1,sw2,PD_blue, PD_green,PD_yellow)
But left side rectangle ( small ) also got changed and looking like circle.I think, I will need to set some flag and will have to decide, which rectangle's corner should be rounded.
But now main concern is PD_blue, PD_green,PD_yellow rectangles color is not visible.
I set their color, after creating rectangle using setBrush(). I do not set their color in paint(). After creating rectangle, if they are full filling some criteria, then I set color on them.
Why that color is not visible ?
Here is my paint()
void guiSchematicRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); QRectF rect = QRectF(_polyRect); QPainterPath path; path.addRoundedRect(rect, 4, 4); auto copied_option = *option; copied_option.state &= ~QStyle::State_Selected; auto selected = option->state & QStyle::State_Selected; painter->save(); if (selected) { painter->setBrush(Qt::NoBrush); painter->setPen(QPen(option->palette.windowText(), 1, Qt::SolidLine)); } else { painter->setPen(Qt::blue); } painter->drawPath(path); painter->restore(); }
-
@JoeCFD
If I add below line in paint() , then I get my 3 rectangle ( PD_blue, PD_green, PD_yellow) with thier respective color.QGraphicsRectItem::paint(painter, &copied_option, widget);
BUt output I get like this :
Can you see the corners ? There is a round shape but it is getting overlapped.
What is the issue with this line ?
QGraphicsRectItem::paint(painter, &copied_option, widget);
With this I get color of remaining rectangle but could not see round shape at edge.
-
@JoeCFD
If I add below line in paint() , then I get my 3 rectangle ( PD_blue, PD_green, PD_yellow) with thier respective color.QGraphicsRectItem::paint(painter, &copied_option, widget);
BUt output I get like this :
Can you see the corners ? There is a round shape but it is getting overlapped.
What is the issue with this line ?
QGraphicsRectItem::paint(painter, &copied_option, widget);
With this I get color of remaining rectangle but could not see round shape at edge.
-
@tushu QGraphicsRectItem::paint(painter, &copied_option, widget); is to call the parent to paint the rect without round corners. Look at the example and this call is not needed.
@JoeCFD
You are correct.
But now problem is, after creating rectangle, for some of the rectangle, I am setting some colour withSetBrush()
And the color applied with above method, is not visible to the rectangle. They are appearing colorless.
Can we use paint() parameter in this case ?
paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Or is there any other way to solve this issue ?
-
@JoeCFD
You are correct.
But now problem is, after creating rectangle, for some of the rectangle, I am setting some colour withSetBrush()
And the color applied with above method, is not visible to the rectangle. They are appearing colorless.
Can we use paint() parameter in this case ?
paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Or is there any other way to solve this issue ?
-
myRect.h
class myRect : public QGraphicsRectItem { explicit myRect(QRectF &rectPoints, QGraphicsScene *_scene, QGraphicsItem *parent = nullptr) : QGraphicsRectItem(rectPoints, parent), _scene(_scene) {} }
myRect.cpp
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(widget); QRectF rect = QRectF(_polyRect); QPainterPath path; path.addRoundedRect(rect, 4, 4); auto copied_option = *option; copied_option.state &= ~QStyle::State_Selected; auto selected = option->state & QStyle::State_Selected; painter->save(); if (selected) { painter->setBrush(Qt::NoBrush); painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine)); } else painter->setPen(Qt::blue); painter->drawPath(path); painter->restore(); } void myRect::CreateRect(QRectF rect) { this->_polyRect = rect; QPen mPen; mPen.setWidth(1); mPen.setBrush(Qt::blue); this->setPen(mPen); this->setFlag(QGraphicsItem::ItemIsSelectable); }
mySymbol.cpp
DrawSymbol() { QRectF Rect( co-ordinates ); myRect* rect = new myRect(Rect, _scene); rect->CreateRect(Rect); if( if it is power domain ) CreatePD(rect); } CreatePD(myRect* rect) { QColor pdColor{some color }; rect->setBrush(pdColor); }
Note : If I add following line in paint()
QGraphicsRectItem::paint(painter, &copied_option, widget);
then I can see my rectangle with color. But gets problem with rounded corner.
-
myRect.h
class myRect : public QGraphicsRectItem { explicit myRect(QRectF &rectPoints, QGraphicsScene *_scene, QGraphicsItem *parent = nullptr) : QGraphicsRectItem(rectPoints, parent), _scene(_scene) {} }
myRect.cpp
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(widget); QRectF rect = QRectF(_polyRect); QPainterPath path; path.addRoundedRect(rect, 4, 4); auto copied_option = *option; copied_option.state &= ~QStyle::State_Selected; auto selected = option->state & QStyle::State_Selected; painter->save(); if (selected) { painter->setBrush(Qt::NoBrush); painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine)); } else painter->setPen(Qt::blue); painter->drawPath(path); painter->restore(); } void myRect::CreateRect(QRectF rect) { this->_polyRect = rect; QPen mPen; mPen.setWidth(1); mPen.setBrush(Qt::blue); this->setPen(mPen); this->setFlag(QGraphicsItem::ItemIsSelectable); }
mySymbol.cpp
DrawSymbol() { QRectF Rect( co-ordinates ); myRect* rect = new myRect(Rect, _scene); rect->CreateRect(Rect); if( if it is power domain ) CreatePD(rect); } CreatePD(myRect* rect) { QColor pdColor{some color }; rect->setBrush(pdColor); }
Note : If I add following line in paint()
QGraphicsRectItem::paint(painter, &copied_option, widget);
then I can see my rectangle with color. But gets problem with rounded corner.
-
-
@tushu said in How to draw rectangle with all corners rounded ?:
if they are full filling some criteria, then I set color on them.
Why that color is not visible ?fill your form first:
painter->fillPath(path, yourColor);