5

我想在 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个同心圆。当我放大填充物时,边界会变得越来越大。知道如何解决这个问题吗?谢谢!

4

1 回答 1

3

相反,我会看ItemIgnoresTransformations标志,这将使项目本身“化妆品”,而不仅仅是笔。这是一个工作示例:

#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();
}
于 2012-05-30T22:00:48.697 回答