PDA

View Full Version : Rendering items to graphicsscene



hema
20th July 2011, 07:24
hi,

i want to add number of ballons(GraphicsItem) into graphicsScene depending upon the position in main.cpp given.
main.cpp

#include<QtGui>
#include"ballon.h"

int main(int argc,char **argv)
{
QApplication app(argc,argv);
QGraphicsScene scene;
scene.setSceneRect(0,0,300,300);
QGraphicsRectItem *rectItem=new QGraphicsRectItem(0,0,300,30);
scene.addItem(rectItem);
QGraphicsTextItem *textItem=scene.addText("After / Before",QFont("",18,QFont::Bold));
scene.setBackgroundBrush(Qt::yellow);
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
for(int i=0;i<5;i++)
{
Ballon *ball=new Ballon("H");
ball->setPos(200,200);
scene.addPath(ball->shape());
}
QGraphicsView view(&scene);
view.setRenderHint(QPainter::Antialiasing);
view.setCacheMode(QGraphicsView::CacheBackground);
view.setViewportUpdateMode(QGraphicsView::Bounding RectViewportUpdate);
view.show();
return app.exec();
}


ballon.cpp

#include "ballon.h"
#include<QFont>

Ballon::Ballon(QString text): _mtext(text)
{
setPos(270,270);
}
void Ballon::setText(QString text)
{
_mtext=text;
}
QString Ballon::getText()
{
return _mtext;
}
void Ballon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

}
QPainterPath Ballon::shape() const
{
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}
QRectF Ballon::boundingRect() const
{
return QRectF(40,40,30,30);
}


ellipse is drawing at boundingRect dimensions but not at ball.setpos(200,200)
i went thru examples but i dont find where the mistake lies.
please help me

mcosta
20th July 2011, 07:43
QGraphicsPathItem * QGraphicsScene::addPath ( const QPainterPath & path, const QPen & pen = QPen(), const QBrush & brush = QBrush() )

Creates and adds a path item to the scene, and returns the item pointer. The geometry of the path is defined by path, and its pen and brush are initialized to pen and brush.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

you have to set pos after add to scene