在QT中绘制一个装饰化妆品的椭圆

我想在QT中绘制一个填充的椭圆,在放大和缩小时不会改变它的大小.现在我有以下内容:

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);

当我缩小边界和填充之间的间隙时.所以它看起来像2个同心圆.当我放大填充物时,边界越来越大,磁盘变得越来越大.知道如何解决这个问题吗?谢谢!

最佳答案 相反,我会寻找
ItemIgnoresTransformations flag,这将使项目本身“化妆”,而不仅仅是笔.这是一个有效的例子:

#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();
}
点赞