PDA

View Full Version : need help with QGraphicItem



rachana
29th January 2007, 09:11
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

jpn
29th January 2007, 09:19
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:



QRectF Triangle::boundingRect() const
{
return shape().boundingRect();
}

QPainterPath Triangle::shape() const
{
// QPointF p1, p2 and p3 are the points of the triangle
QPainterPath path;
path.addPolygon(QPolygonF() << p1 << p2 << p3);
return path;
}

rachana
29th January 2007, 10:10
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,

aamer4yu
29th January 2007, 10:42
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 -

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

rachana
29th January 2007, 12:45
But while rotating the triangle , the lines of the triangle becomes hazy,
can some body suggest how to solve this? like this

Gopala Krishna
29th January 2007, 15:34
You just need to enable antialiasing in the view. Add this in your MainWindow's constructor.

view->setRenderHint(QPainter::Antialiasing);

rachana
29th January 2007, 16:16
I tried still facing the same problem :(

Gopala Krishna
29th January 2007, 16:27
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 ?

rachana
30th January 2007, 03:29
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);

aamer4yu
30th January 2007, 04:01
Set the antialiasing in the paint function of traingle class...

painter->setRenderHint ( QPainter::Antialiasing);

rachana
30th January 2007, 05:12
It is also set I dont know why the sides of triangle becomes jagged once its rotated :(

aamer4yu
30th January 2007, 05:34
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 ??

rachana
30th January 2007, 06:04
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

aamer4yu
30th January 2007, 06:27
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.