need help with QGraphicItem
Hi all,
I have problem working with QGraphicItem. I am drawing a triangle with sides,(a=84,b=84and c=116) I dont know how to specify the bounding Rect() and shape()
QRectF Traingle::boundingRect() const
{
//return QRectF(, , , ,); //What value should be specified here ??
}
QPainterPath Triangle::shape() const
{ QPainterPath path;
//what should be written here?
return path;
}
Actually I want to rotate the triangle, but not sure how to specify the boundingRect() and shape(), can some body please explain?
Rachana
Re: need help with QGraphicItem
Hi, maybe you could make use of QGraphicsPolygonItem? It would calculate the bounding rect and the shape for you. Anyway, you can use QPainterPath to calculate both:
Code:
QRectF Triangle
::boundingRect() const {
return shape().boundingRect();
}
{
// QPointF p1, p2 and p3 are the points of the triangle
path.
addPolygon(QPolygonF() << p1 << p2 << p3
);
return path;
}
1 Attachment(s)
Re: need help with QGraphicItem
hi I am able to draw the triangle, but I want to rotate it on right click, even though it rotates it
never rotates on the centre it moves all over the place,
can some body give hint like whats wrong here? I am attaching the source file,
Re: need help with QGraphicItem
Nothings wrong... just the centre axis...
The items are rotated on axis of (0,0). So when you right click, they are rotated in relation to (0,0).
Just change your points in shape() and boundingRect() to -
Quote:
QPointF p1(0,-40);
QPointF p2(-40,40);
QPointF p3(40,40);
Now (0,0) will be in centre of the triangle and you will get the result you want :)
1 Attachment(s)
Re: need help with QGraphicItem
But while rotating the triangle , the lines of the triangle becomes hazy,
can some body suggest how to solve this? like this
Re: need help with QGraphicItem
You just need to enable antialiasing in the view. Add this in your MainWindow's constructor.
Code:
view
->setRenderHint
(QPainter::Antialiasing);
Re: need help with QGraphicItem
I tried still facing the same problem :(
Re: need help with QGraphicItem
But it showed up properly when I tried it. Are you sure there is no improvement ? Can you please post the pic now for comparison ?
1 Attachment(s)
Re: need help with QGraphicItem
here is the pic and code I have modified,
view = new QGraphicsView(scene); //make sure always pass the QGraphicsScene
view->setSceneRect(-400,-400,800,800);
view->setRenderHint(QPainter::Antialiasing);
setCentralWidget(view);
Re: need help with QGraphicItem
Set the antialiasing in the paint function of traingle class...
painter->setRenderHint ( QPainter::Antialiasing);
Re: need help with QGraphicItem
It is also set I dont know why the sides of triangle becomes jagged once its rotated :(
1 Attachment(s)
Re: need help with QGraphicItem
its working fine for me...
see the a attached pic..
Are they smooth in ur perception ?? If not what exactly do u mean by jagged edges ?
Also can u post ur Triangles::paint() function ??
Re: need help with QGraphicItem
void Triangle:: paint ( QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
//need to give some colors & may use some gradient here :)
painter->setPen(QPen(Qt::black,0, Qt::SolidLine,Qt::FlatCap, Qt::MiterJoin));
QPointF p1(0,-40);
QPointF p2(-40,40);
QPointF p3(40,40);
painter->drawPolygon(QPolygonF() << p1 << p2 << p3);
}
I mean to say the sides of triangle never appears straight on rotation, they appear like zig zag pattern
Re: need help with QGraphicItem
does the snapshot i posted appear jagged ?? They appear smoother than the one u posted before.
Still there will be slight jagging, you cannot draw very straight line between points of different height.
Also try setting the pen width to 2-5 if you want. This will give more smooth lines, as more no of pixels. Also take care of bounding rect if pen width is too much.