PDA

View Full Version : force redraw of every graphicsitem



Morea
15th January 2007, 15:33
I'm having a little problem with redrawing in a graphicsview/scene.
If I scroll to another position of the scene, there are some graphicsitems that are not redrawn.
See the picture. There you can see how the boxes are redrawn, but the lines with the integers on are not always drawn, 4 of them are just started, but not finished.

The integers are QGraphicsTextItems that has a customized paint() function that draws a line beneth the integer.

I'm not sure how to make it do a proper redraw of the scene.

Morea
15th January 2007, 15:45
I added

void MainWindow::resizeEvent(QResizeEvent* e)
{
scen->update(scen->sceneRect());
}

to the mainwindow class, but even this doesn't handle redrawing properly.

B.t.w, what setting should I use to make the graphicsview NOT scrolling back to some strange default setting if I scroll to the right for example and add something that changes the scene (then the view scrolls to the left corner).

wysota
15th January 2007, 17:12
Does boundingRect() return proper rects for all the items?

Calling update() won't do anything if the items have invalid bounding rects.

Morea
15th January 2007, 17:28
I added this:

QRectF Svar::boundingRect() const
{
return QRectF(-30,-20,60,40);
}

Svar is derived from QGraphicsTextItem.

It stills seems to be some problem, It is difficult to double click on the Svar items to. If I click like a frustrated user, then I might hit it with a click. Really strange, but perhaps related?

wysota
15th January 2007, 17:38
When implementing custom graphics items, you have to make sure boundingRect() and shape() return proper data:
- boundingRect should return a rectangle which should be marked as dirty and redrawn when the item changes
- shape should return a shape of the item you wish to use for collision detection (which is used for "clicking" on an item as well).

In your situation make sure boundingRect and shape() take into consideration the lines you added.

BTW. Maybe you should inherit from QGraphicsAbstractShapeItem instead of QGraphicsTextItem?

Bitto
18th January 2007, 19:18
Make sure you call QGraphicsItem::prepareGeometryChange() if you ever change the item's bounding rect (i.e., the return value of boundingRect()).

http://doc.trolltech.com/4.2/qgraphicsitem.html#prepareGeometryChange