2012-05-30 4 views
4

확대/축소시 크기를 변경하지 않는 채워진 타원을 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 개의 동심원처럼 보입니다. 채우기가 확대되면 경계가 지나치게 커지고 디스크가 커집니다. 이 문제를 어떻게 해결할 수 있습니까? 감사!

답변

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(); 
} 
+0

단순히 'QGraphicsItem :: ItemIgnoresTransformations'를 추가하면 타원이 사라집니다. 구글의 빠른 검색은 같은 문제를 가진 사람들을 보여주었습니다. 혹시 문제가 무엇인지 알 수 있습니까? – shalabuda

+0

@shalabuda 잘 모르겠습니다. 내 답변을 편집하여 실습 예제를 포함 시켰습니다. 아마도 예제로 시작하여 이것이 구현과 어떤 관련이 있는지 볼 수 있습니다. – Anthony

+0

예를 들어 주셔서 감사합니다! – shalabuda