PDA

View Full Version : QGraphicsItem is not drawn again?



zgulser
16th October 2009, 11:30
Hi all,

I have the following problem;

I have a scene and items in it. Once I zoom in the scene, some of the items go off the scene normally. But when I zoom out, I don't the items that are there before I zoomed in.

What could be the possible problem?

high_flyer
16th October 2009, 11:37
What happens if you zoom so, that for example an item is only half out site the view area, and then zoom back, will you see the whole object item again, or only the part that was not zoomed "out"?

zgulser
16th October 2009, 13:05
If only a part of an item is seen after zooming in, then no problem after zooming out. If the item is completely off the scene after zooming in, then nothing appears about the item.

Is it more clear now?

high_flyer
16th October 2009, 13:41
can you show your zooming code?

zgulser
16th October 2009, 14:05
I'm afraid I can't.

But you see the problem is not about zooming. I guess I failed in updating of the relevant items issue or the scene itself.

high_flyer
16th October 2009, 14:06
But you see the problem is not about zooming. I guess I failed in updating of the relevant items issue or the scene itself.

Probably, but that should be visible in the zooming code, since then we can see what happens when you zoom out again...

But without code it very hard to help...

zgulser
16th October 2009, 14:12
Batu olm bi gavra hele ya felaket sıkıldım. batu olmazsa seyhun o da olmazsa bu işin ordinasyüsü levent.

zgulser
19th October 2009, 10:42
The problem here is basicly because of item's removal from the scene bounding rect. So how can I put the item back into the scene rectangle when it's gone out of it by zooming in action , if I zoom out in the scene again?

scascio
19th October 2009, 11:05
Why are you managing yourself the items viewed in your scene?

Since Qt do the job for you and compute the visible items based on QGraphicsItem::boundingRect, you will get in QGraphicsView::drawItems the list of visible items.
So no need to remove or add items.

Are you computing the zoom yourself in the scene, in your custom items or elsewhere?

zgulser
19th October 2009, 13:01
I guess you misunderstood me. I didn't remove or add items when I zoom in or zoom out. This is what I think as a possible reason that the items are no longer visible once they are off the scene's bounding rectangle. It is also strange that the program still enters item's paint method although it's not drawn.

I deal zooming stuff in the view by the way.

scascio
19th October 2009, 13:41
So that means that the item are seen by Qt, according to its bounding rect.

Can you post a simplified version of your zoom function?

There is probably a coordinates conversion error of your items position, so they are drawn out of the viewed rect.

Another questions :
* Why not using Qt built-in QGraphicsView::scale?
* Do you manage derived QGraphicsItem with overloaded paint?

zgulser
19th October 2009, 15:44
Hi,

sorry but I can't because I'm using different library for such operations(zooming, rotating etc.) and the functions are hidden which means not available. I just called setRange(+/- rangeAmount) and it does. Sorry fot that:(.

By the way, don't you think that items would have drawn wrongly at the begining or at the situations in which the items are in still scene's rectangle if there is a coordinate error?

Anyway regarding your last questions,

I didn't prefer to use it because I deal with geographic coordinates( latitudes and longitudes) so that's why we use different library as I mentioned.

And I don't think my paint functions are overloaded. They're just dealing with paint issues.

scascio
19th October 2009, 17:26
By the way, don't you think that items would have drawn wrongly at the begining or at the situations in which the items are in still scene's rectangle if there is a coordinate error?

That why we can help you if you provided some code. Maybe in your loop there is something obvious you don't see now. Maybe your are cumulating zoom scale factor instead of applying it globally, maybe it is the result of integer division...

Can't you even sum up the data life cycle : creation and adding to the scene, setting position and geometry? It is hard to help you without knonwing what your are doing.

If you never scale the view by native Qt method scale, I guess you are overloading the paint method of the view and compute yourself relative position of items and their size in pixel coordinates. But then it is only painting, isn't it?
In that conditions what is the difference with drawing in a QWidget?



I didn't prefer to use it because I deal with geographic coordinates( latitudes and longitudes) so that's why we use different library as I mentioned.

It is only a matter of coordinate system reference. You only need to choose one for you scene, and convert item coordinates when adding.
Since 1 unit is 1 pixel at scale 1, you can fix your system projection and unit, then position your items and set their size. Then let Qt do the rendering of visible item for you.
All you have to do it convert your data when creating items and painting them, according to the projection you are using for your scene : plane projection for latitude/longitude in degrees or seconds, or UTM or another metric projection

An easy way to use QGraphicsView is to choose a projection system for your scene, geographic or metric.

So you can derived QGraphicsItem :
* when you add it on the scene, just set a member data to your item (passing in constructor for instance)
* set its position in scene coordinate (using your conversion libraries),
* implement the QGraphicsItem::boundingRect, that return the rect in scene coordinate (you can use your libraries to compute it and even optimize in your object the way to do it only once,
* overload QGraphicsItem::paint. The paint method gets in its arguments the rect of visible portion of your data ( in scene coordiante so in the projection you have chosen) and the level of detail (the scale factor) set by the QGraphicsView::scale. You can then use in paint event the library you want you draw in the painter, in relative pixel coordinate of the item. If any part of the rect is visible in the scene, a paint is triggered.


Therefore, picking and moving items, zooming and navigating through the view is managed by Qt and call paint for item that need to be displayed.

Instead of concentrate on the view, can't you focus on items?

This may save you a lot of work, don't you think?