PDA

View Full Version : problem in positioning item in QGraphicsScene()



wagmare
7th May 2009, 08:38
hi friends,
i have a problem in positioning an rect item (0, 0, 20, 150) in a graphicsScene() of size (0, 0, 1024, 1024)



DiagView::DiagView(QWidget *parent)
: QGraphicsView(parent)

{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
setCacheMode(CacheBackground);
setViewportUpdateMode(FullViewportUpdate);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
setStyleSheet("background:#336699");
QRectF bounds(0, 0, 1024, 1024);
scene = new QGraphicsScene(bounds, this);
setScene(scene);

item1 = new SlotItem(QRectF(0, 0, 20, 150));
item1->setPos(10,180);
scene->addItem(item1);
}
void DiagView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}

slotitem.cpp


SlotItem::SlotItem(const QRectF &rect)
: QGraphicsRectItem(rect)
{

}
void SlotItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->setBrush(QColor(0, 102, 153));
painter->drawRoundRect(rect());

}
QRectF SlotItem::boundingRect()
{
return QRectF(0,0, 50,300);

}


main.cpp


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

DiagView *dialog = new DiagView();
dialog->setGeometry(0, 0, 1024, 1024);
dialog->show();
return app.exec();
}


but the SlotItem is shown in the center of the view port because of the resize event .. if i dont use resize event it positioned the item acurately ... but i need the resize event in future ... what went wrong in my code .. please any one help me ...

wysota
7th May 2009, 10:27
I'm a bit confused. First you pass a rectangle to the QGraphicsRectItem constructor but then you reimplement boundingRect() and return a different rectangle. What's the point of doing that? The two rectangles should be identical.

About your problem - try changing the aspect parameter of the fitInView call.

wagmare
8th May 2009, 07:34
sorry i will change soon the bounding rect value ....
sir in my view i set the geometry to (1024, 1024) and QGraphicsScene also has the same and i positioned the item and it works correctly ... i reimplement the resize event then now the item displayed in centre when i use setPos(0, 30) in my scene .... please help ...

wysota
8th May 2009, 07:39
Please read my post again.

wagmare
8th May 2009, 07:41
try changing the aspect parameter of the fitInView call.
thanks ... i will