PDA

View Full Version : setSceneRect not being called properly?



bjh
19th March 2008, 17:45
Hi All,

Using Qt4.3 I’m using a QGraphicsScene and QGraphicsView to show small portion of a large image (4600x6000 pix) and draw symbols upon it. Works fine (AFAIK). I then use the same scene and view to show the same size portion of a larger image (8000, 5000). The symbols I draw do not show up, and paint is only called once. But if I scroll my scene to the left, about half-way through, paint starts getting called again, but when I scroll back to my starting point, no symbols are drawn.

It seems like I am not defining my view or scene properly, but I don’t see how to do it differently. I’m assuming as I scroll to the left and get to the half-way, my view is then showing the range of the first image, which was 4600 pixels wide.

For simplicity, I’m leaving out details of the classes which are doing the painting, etc.

Code snippet is below. Does anybody see what am I doing wrong?

bjh



// first image
QGraphicsScene *scene;
QGraphicsView *view;

view->setBackgroundBrush(QPixmao(image1));
scene->setSceneRect(0, 0, 4600, 5500)
view->resize(400, 300);
// symbols drawn…

// second image
scene->setSceneRect(0, 0, 8000, 5000);
view->updateSceneRect(QRectF(0, 0, 8000, 5000));
view->setbackgroundBrush(QPixmap(image2));
// symbols not drawn…

bjh
3rd April 2008, 22:01
Hi All,

Just found the root of my problem. I forgot to change my boundingRect() method when I changed the scene rectangle! So when I switched to a wider rectangle for the scene, only the old width of it was 'bounded'!

Code now appears to work as expected; hope this post may save someone else from pulling out their hair! ;-)


bjh

wysota
3rd April 2008, 23:42
The bounding rect should be calculated in item's local coordinates. Global coordinates should be set using QGraphicsItem::setPos(). You probably didn't use it and that caused your problems.

qtzcute
12th July 2009, 13:47
I could not understand the relation between setSceneRect and bounding rectangle.

I need to display a no. of rectangles each placed right to the previous rectangle. Now, as number of rectangles is high therefore i needed a scroll bar, so that i may scroll right to see rest of the rectangles.

My GraphicsView size geometery is

graphicsView->setGeometry(QRect(10, 8, 730, 540));

so i set my screen rectangle as:

scene->setSceneRect(-360,-266,1000,532);
because 730-10 = 720 and 720/2 = 360. Similarly 540-8=532 and 532/2 = 266

I set x2 = 1000 and not 720 because i wanted horizental scroll bars to appear.

Eace time i add a rectangle i pass its size too. I made a tiny class for displaying my rectangles. When i execute my program the rectangles just blink and disappear immediately. The scroll bars do appear but i can't see any of my rectangles. Please help me out. I can't figure out the problem but i am trying. :(



#include "rectMap.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include <math.h>
static int xLocation = -360;

RectMap::RectMap(int Size)
:color(qrand() % 256, qrand() % 256, qrand() % 256)
{
size = Size;
}

QRectF RectMap::boundingRect() const
{
//.................................................. .................................................. .......
I really do not have any idea wat my bounding rectangle should be
//.................................................. .................................................. .......
qreal adjust = 0.5;
//return QRectF(-18 - adjust, -22 - adjust,36 + adjust, 60 + adjust);
//return QRectF(-xLocation, -262, size, 523);
return QRectF(-400,-262,1000,532);
}

QPainterPath RectMap::shape() const
{
QPainterPath path;
path.addRect(-100, -200, 20, 40);
return path;
}

void RectMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->setBrush(color);
painter->drawRect(xLocation,-262,size,523);
updateLocation(size);
}

void RectMap::updateLocation(int size)
{
xLocation = xLocation + size+ 3;
}

wysota
12th July 2009, 17:36
This code is surely invalid. Changing location as a result of a repaint is probably not what you want.

qtzcute
12th July 2009, 19:37
Sorry but i am very new in Qt's world and could not understand what you said. Can u kindly tell me where am i changing the location?

If you are talking about updating the 'xLocation' integer variable in paint function then well, i am doing that because i want each new rectangle to be drawn at new place.

If not, then please guide as to what i need to do.

Thanks a lot wysota

wysota
12th July 2009, 20:34
Sorry but i am very new in Qt's world and could not understand what you said. Can u kindly tell me where am i changing the location?
In the paint() routine.


If you are talking about updating the 'xLocation' integer variable in paint function then well, i am doing that because i want each new rectangle to be drawn at new place.
That's surely not the right way to do this. paint() will be called for each of the rectangles more than once.

In my opinion you should stick to QGraphicsRectItem or its subclass.

qtzcute
12th July 2009, 20:42
Many thanks wysota...problem solved.

You were right i should not have change the position in paint function...Now my rectangles are appearing quite finely. :D