2017-12-28 15 views
1

QGraphicsItem을 다시 정의하여 LineString으로 표시했습니다. 내 자신을 boundingbox 만들고 화가가 추상 메서드를 다시 정의해야하기 때문에 이것을 다시 정의했습니다.Qt의 기울기가있는 라인의 경계선

지금이 코드가 있습니다

QRectF myQGraphicsLine::boundingRect() const 
{ 
    double lx = qMin(myLine->getX(0), myLine->getX(1)); 
    double rx = qMax(myLine->getX(0), myLine->getX(1)); 
    double ty = qMin(-myLine->getY(0), -myLine->getY(1)); 
    double by = qMax(-myLine->getY(0), -myLine->getY(1)); 
    return QRectF(lx-size/2,ty, rx -lx + size, by-ty).; 
} 

void myQGraphicsLine::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) 
{ 
    pen.setColor(Qt::red); 

    QLine line(myLine->getX(0), -myLine->getY(0), myLine->getX(1), -myLine->getY(1)); 
    pen.setWidth(size); 

    painter->setPen(pen); 

    painter->setBrush(brush); 
    painter->drawLine(line); 
} 

이 모든 작업의 ​​벌금을,하지만 난 boundingRec에 약간의 문제가 있습니다. 선이 x 축 또는 y 축을 따르는 경우 나는이 결과 얻을 :
BoundingRec is black line

를 다른 위치에 나는이 얻을 :

enter image description here

을하고 나는이 필요합니다

enter image description here

boundinRec을 돌리는 방법을 아는 사람이 있습니까? 고마워요!

+2

체크 아웃 http://doc.qt.io/qt-5/qgraphicsitem.html#boundingRegion – hyde

+0

감사합니다. @hyde, 확인하겠습니다. – Zharios

답변

1

I QGraphicsItem::shape() 메서드를 재정 의하여 문제를 해결합니다. 방법은 다음과 같이 체재()

void myQGraphicsLine::createSelectionPolygon() 
{ 
    QPolygonF nPolygon; 
    QLineF line(myLine->getX(0), -myLine->getY(0), myLine->getX(1), -myLine->getY(1)); 
    qreal radAngle = line.angle()* M_PI/180; //This the angle of my line vs X axe 
    qreal dx = size/2 * sin(radAngle); 
    qreal dy = size/2 * cos(radAngle); 
    QPointF offset1 = QPointF(dx, dy); 
    QPointF offset2 = QPointF(-dx, -dy); 
    nPolygon << line.p1() + offset1 
     << line.p1() + offset2 
     << line.p2() + offset2 
     << line.p2() + offset1; 
    selectionPolygon = nPolygon; 
    update(); 
} 

그들이 (페인트), boundingRect() 모양 :

이 사용하기 위해, 나는 내 라인 모양으로 QPoligonF을 만들어 내 경우에는 내가이 기능을 사용

QRectF myQGraphicsLine::boundingRect() const 
{ 
    return selectionPolygon.boundingRect(); 
} 

void myQGraphicsLine::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) 
{ 
    pen.setColor(Qt::red); 

    QLineF line(myLine->getX(0), -myLine->getY(0), myLine->getX(1), -myLine->getY(1)); 
    pen.setWidth(size); 
    painter->setPen(pen); 
    painter->setBrush(brush); 
    painter->drawLine(line); 
} 

QPainterPath myQGraphicsLine::shape() const 
{ 
    QPainterPath path; 
    path.addPolygon(selectionPolygon); 
    return path; 
} 

답장을 보내 주셔서 감사합니다.

0

QRect 또는 QRectf는 회전 할 수 없습니다. 그 변은 항상 수직 및 수평 축에 평행합니다. 이것을 사각형의 왼쪽 위 모서리에 '너비'및 '높이'멤버를 더한 점 (x, y)으로 생각하십시오. QRect에서 수행하는 변환이 무엇이든 관계없이 항상 '너비'를 가로 거리로, '높이'를 세로 거리로 해석합니다.

QRect의 네 모서리를 포인트로 추출한 다음 해당 포인트를 회전시킬 수는 있지만 회전 된 포인트를 QRect로 다시 변환 할 수는 없습니다 (뷰포트/창문.