Draw a cosmetic filled ellipse in QT

I want to draw a filled ellipse in QT that will not change its size when scaling and exiting. At the moment, I have the following:

QPen pen = painter->pen(); pen.setCosmetic(true); pen.setWidth(5); painter->setPen(pen); QBrush brush = painter->brush(); brush.setStyle(Qt::SolidPattern); painter->setBrush(brush); painter->drawEllipse(p, 2, 2); 

When I reduce the gap between the border and the fill, will appear. So it looks like 2 concentric circles. And when I increase the filling, it overflows the border, and the disk gets bigger and bigger. Any idea how to fix this? Thanks!

+4
source share
1 answer

Instead, I would look at the ItemIgnoresTransformations flag , which will make the element itself "cosmetic", and not just a pen. Here is a working example:

 #include <QtGui> class NonScalingItem : public QGraphicsItem { public: NonScalingItem() { setFlag(ItemIgnoresTransformations, true); } QRectF boundingRect() const { return QRectF(-5, -5, 10, 10); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QPen pen = painter->pen(); pen.setCosmetic(true); pen.setWidth(5); pen.setColor(QColor(Qt::red)); painter->setPen(pen); QBrush brush = painter->brush(); brush.setStyle(Qt::SolidPattern); painter->setBrush(brush); painter->drawEllipse(QPointF(0, 0), 10, 10); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QGraphicsScene *scene = new QGraphicsScene; QGraphicsView *view = new QGraphicsView; NonScalingItem *item = new NonScalingItem; scene->addItem(item); view->setScene(scene); /* The item will remain unchanged regardless of whether or not you comment out the following line: */ view->scale(2000, 2000); view->show(); return app.exec(); } 
+2
source

Source: https://habr.com/ru/post/1415255/


All Articles