PDA

View Full Version : Zooming scene Rect(Qt bug?)



maverick_pol
17th August 2007, 09:43
Hi guys,

I have 2 problem connected with zooming the scene.
First of all, I am using the code beneath to zoom the given rect, but it alwas zoom the center(at the begining, for the first 2 times).


void MyCLass::vZoomSceneRectangle(QRectF _rectangle)
{
m_view->fitInView(_rectangle,Qt::KeepAspectRatio); // or Qt::KeepAspectRatioBy expanding
m_view->ensureVisible(_rectangle);
}


The second problem is connected with fixed position. I have created QLabel as view->viewport() children(to set fixed position). When I scale the view(scale(2.0,20)), for the first 4 times their position is ok, but when I zoom In for the 5 time they change their position. So i am deleting them and recreating everytime they I zoom the scene rect and their position is ok(even though there are some painting lags).
But when I use fitInView(QRectF) method my QLabel position changes and I am back where my problems appeared.

Any idea how to zoom the chosen rectangle(correctly); I am currenly reading selected rectangle from the scene and use fitInView(QRectF..).
How to force my widgets to ignore zooming(I belive it should be a default action , when I created them as children of the view->viewport()).

For any ideas thank YOU.

Maverick

wysota
17th August 2007, 12:38
How did you initialize the scene? Did you set the range of coordinates for it?

maverick_pol
17th August 2007, 12:45
I have implemented my own custom scene.
Here is the initialization.


m_scene = new GScene(this);
m_scene->setSceneRect(-50,50,m_view->width(),m_view->height());
m_scene->setBackgroundBrush(QBrush(Qt::gray));
m_scene->setItemIndexMethod(QGraphicsScene::BspTreeIndex);


Maverick

wysota
17th August 2007, 12:56
And how do you call the zooming method? A minimal compilable example reproducing the problem would help...

maverick_pol
17th August 2007, 13:02
I have a parent class which contain scene and view objects.
When I choose a rectangle for zooming I emit a signal which is connected to parent slot.
SIGNAL:


emit vSignalZoomRectangle(m_zoomPolygonItem->boundingRect());
//m_zoom PolygonItem is a QPolygonItem created in the scene coordinates(the rect for zooming)

SLOT:


void MyClass::vZoomSceneRectangle(QRectF _rectangle)
{
m_view->fitInView(_rectangle,Qt::KeepAspectRatio);
//m_view->ensureVisible(_rectangle);



the connection is ok, and the correct rectF is passed, but no matter the rect coordinates the first zooming in ans other zoom the scene center point.

?

wysota
17th August 2007, 13:06
What are the exact numbers of the scene size and the rect you pass to fitInView?

maverick_pol
17th August 2007, 13:27
It depend because I have a rectangle moving on the scene, and when I press the left mouse button the rectangle boundingRect is being read and passed.
I have checked again and the rectangle passed/received are the same and do change their position.

Maverick

wysota
17th August 2007, 14:06
I'd like to see the numbers though... In my opinion, you create the scene with incorrect coordinates - you use the size of the view, but I suspect the view hasn't been shown earlier, thus it doesn't have its size calculated yet and you are receiving bogus values that later on cause your scene to be always centered in the view (which is a normal behaviour if the scene is smaller than the view). So please use qDebug() and inspect those values.

maverick_pol
17th August 2007, 14:24
Here are Top left/right and bottomLeft/Right points of the rectangles printed by the code in the Slot:


qDebug()<<(_rectangle.bottomLeft().rx())<<(_rectangle.bottomLeft().ry());
qDebug()<<(_rectangle.bottomRight().rx())<<(_rectangle.bottomRight().ry());
qDebug()<<(_rectangle.topLeft().rx())<<(_rectangle.topLeft().ry());
qDebug()<<(_rectangle.topRight().rx())<<(_rectangle.topRight().ry());


1.

-606 -21
-356 -21
-606 -171
-356 -171

2.
340 -76
590 -76
340 -226
590 -226

3.
-135 99
115 99
-135 -51
115 -51

1. 2. and 3. are different coordinates, but after fittingInvView(rect..) I get the same zoom effect.

??

wysota
17th August 2007, 15:35
And the scene size?

maverick_pol
17th August 2007, 18:46
THe scene size is the same before zooming and after the action, but despite the given rectangle the scene looks the same after first zooming.

Maverick

wysota
17th August 2007, 19:40
But what is the scene size? :) Could you show the exact figures - the scene size, the view size, the matrix of the view and the rectangle?

Something like (relative to the view object):

qDebug() << sceneRect() << size() << matrix() << _rectangle;

Bitto
18th August 2007, 07:56
maverick_pol, please just send a compilable self-contained example, and the bug is likely to be revealed right away. It's hard to debug code that's translated to english.

maverick_pol
21st August 2007, 12:28
Hi,

I can't send you the code as I do not have permision to do so. I will send the (rectangle/size/etc) within 15 minutes.

Here it is:

The code I am using to print values:


// the given _rectangle for zooming
qDebug()<<(_rectangle.bottomLeft().rx())<<(_rectangle.bottomLeft().ry());
qDebug()<<(_rectangle.bottomRight().rx())<<(_rectangle.bottomRight().ry());
qDebug()<<(_rectangle.topLeft().rx())<<(_rectangle.topLeft().ry());
qDebug()<<(_rectangle.topRight().rx())<<(_rectangle.topRight().ry());
m_view->fitInView(_rectangle,Qt::KeepAspectRatio); // or Qt::KeepAspectRatioBy expanding
// Scene rectangle
_rectangle = m_view->sceneRect(); qDebug()<<(_rectangle.bottomLeft().rx())<<(_rectangle.bottomLeft().ry());
qDebug()<<(_rectangle.bottomRight().rx())<<(_rectangle.bottomRight().ry());
qDebug()<<(_rectangle.topLeft().rx())<<(_rectangle.topLeft().ry());
qDebug()<<(_rectangle.topRight().rx())<<(_rectangle.topRight().ry());
// scene rectangle(from view)
_rectangle = m_scene->sceneRect(); qDebug()<<(_rectangle.bottomLeft().rx())<<(_rectangle.bottomLeft().ry());
qDebug()<<(_rectangle.bottomRight().rx())<<(_rectangle.bottomRight().ry());
qDebug()<<(_rectangle.topLeft().rx())<<(_rectangle.topLeft().ry());
qDebug()<<(_rectangle.topRight().rx())<<(_rectangle.topRight().ry());
// view size
qDebug()<<m_view->size().height()<<m_view->size().width();
//view matrix
qDebug()<<m_view->matrix().m11()<<m_view->matrix().m12()<<m_view->matrix().m21()<<m_view->matrix().m22()<<m_view->matrix().det()<<m_view->matrix().dx()<<m_view->matrix().dy();


HEre is the result:

-172 38
78 38
-172 -112
78 -112
-50 80
50 80
-50 50
50 50
-50 80
50 80
-50 50
50 50
660 1224
4.34667 0 0 4.34667 18.8935 0 0

QGraphicsScene::removeItem: item 01F45440's scene (00000000) is different from this scene (01AB88D8)

-51.3037 67.408
11.1963 67.408
-51.3037 29.908
11.1963 29.908
-50 80
50 80
-50 50
50 50
-50 80
50 80
-50 50
50 50
660 1224
17.3867 0 0 17.3867 302.296 0 0

QGraphicsScene::removeItem: item 01E9E968's scene (00000000) is different from this scene (01AB88D8)

-30.8857 57.2278
-15.2607 57.2278
-30.8857 47.8528
-15.2607 47.8528
-50 80
50 80
-50 50
50 50
-50 80
50 80
-50 50
50 50
660 1224
67.84 0 0 67.84 4602.27 0 0

QGraphicsScene::removeItem: item 01F78660's scene (00000000) is different from this scene (01AB88D8)

-19.487 52.5501
-15.5808 52.5501
-19.487 50.2064
-15.5808 50.2064
-50 80
50 80
-50 50
50 50
-50 80
50 80
-50 50
50 50
660 1224
271.36 0 0 271.36 73636.2 0 0

I have used zoom In 4 times here and after the second Zoom IN my widgets(created with m_view->viewport() as the parent) moved left. I tried to recreate the widgets after zooming In (removing items and addint them once again) that's why I get the "removeITem" warning.

Maverick

wysota
21st August 2007, 13:14
Looks like I was right from the very beginning - scene coordinates seem incorrect.

BTW. You could have formatted the output better, you know...

maverick_pol
21st August 2007, 13:25
Hi,

I will format the INPUT correctly. Sound good that you were right, but I still do not know what is the problem. Could you explain it to me?
When I use (scale() and fitInView() ) function the coordinates system changes, what kind of incorrectness you're seeing here?
I really appreciate your help in this thread.

Maverick

wysota
21st August 2007, 14:08
Take a look at the coordinates of the zooming rect, the scene and the view during first two zooms. If you have trouble, take a piece of paper and draw the rectangles, taking a closer look at how they intersect each other. Only during the last two sets of coordinates the zooming rectangle is smaller than the scene. A nice thing to have would be to know the bounding rect of all the items in the scene as well...

You should really try with a minimal compilable example as asked at the beginning... Or provide actual code - we won't steal it :)

maverick_pol
22nd August 2007, 07:33
Hi,

Here is the piece of code I am using ( it's 100% what I have in my application(the part connected with gview framework).
After choosing the Zoom in from the Context menu and once again clicking left mouse button the rectangle appears and can be moved with the cursor and resized while pressing the right mouse button.
(There are 2 windows, the one with the gview is mine; other is not used)

My "Panels"(in the fungc class) should be at the same place while zooming, but are moving.
also in the begining(while zooming for first 2...4 times the center of the scene is zoomed not the rectangle).

If you have any ideas I would appreciate your help.

P.S. I have compiled/run this code using Qt 4.3.1 under win32

Maverick

maverick_pol
22nd August 2007, 13:32
Hi guys,

Any ideas what can be the problem?(wrong zooming rectangle and moving Panels(labels))

Maverick

maverick_pol
23rd August 2007, 11:57
Hi guys,

Excuse me for writing again about this problem, but the (my)last thread seems to die.
I have a problem, which I can't solve for over a week, I attach the code and the link to the thread. If anyone could at least compile the code at look where the problem may be and give some ideas I would really appreciate it.

THe problem.

http://www.qtcentre.org/forum/f-qt-programming-2/t-zooming-scene-rectqt-bug-8625.html

Thank you very much for your patience.

Maverick

pdolbey
23rd August 2007, 14:51
I'm not really a QGraphicsView expert and I've only got 4.2.1 at work so I had to hack out some of the hints, but you might find that the odd "update();" at the end of your zoomin, zoomout and mousemove routines might help a bit.

Pete

maverick_pol
23rd August 2007, 15:11
all ideas are usefull.

I will try using "update()" and give you the result, but this probably will not solve all the problems.

Maverick

jacek
23rd August 2007, 20:27
Excuse me for writing again about this problem, but the (my)last thread seems to die.
Please don't start more than one thread on the same topic.

maverick_pol
24th August 2007, 08:35
Sorry. I should rather try to "refresh" the thread.

using "update()", does not give any improvement.
Still looking for a solution. Ideas appreciated.

Maverick

jpn
24th August 2007, 10:09
Alright, I gave it a try but I'm sorry to say I soon gave up. A couple of clear errors:

a QGraphicsView object allocated on the stack is being deleted => crash
a QGraphicsView* is incorrectly casted as GView*, an actual GView is never created (once you add a member to GView and try to call it => crash)

Other comments:
I'm not saying any of these particular issues would cause the misfunctionality, but things like public variables and first setting QGraphicsScene::NoIndex in GScene constructor but then switching it immediately to QGraphicsScene::BspTreeIndex in FundGC just make it hard to follow what is being modified and where, and who controls what. Sorry, but the attached code is a bit too "bloated" for an outsider to quickly see what's going on.

maverick_pol
24th August 2007, 13:11
Ok, I will correct the mistaked right away.
But do you have ideas why the zooming is wrong ?

I understand that the code may not be as clear and high quality as at your level of coding, but I would like you, if it is not a problem, to look at 2 methods.

fundgc.cpp ->FundGC::vZoomSceneRectangle(QRectF _rectangle);
scene.cpp ->GScene::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent );

mousePressEvent passes the rectangle for zooming(

emit vSignalZoomRectangle(m_zoomPolygonItem->boundingRect());
and the vZoom... zooms the rectangle. There is an error in the this methods or scene settings, which I can't find on my own.

A -> base class
B: public A

dynamic_cast<B*>(A*) <- why this casting will not work? ( or static)

I really appreciate your help and ideas.

Maverick

maverick_pol
25th August 2007, 12:54
Hi,

I have na idea where the problem lays(I suppose that wysote suggested it at the beginning in the first place, but didn't understand the idea). I belive the problem is connected with scene/view sizes.
Here is the information about the view/scene sizes after construction and their sizes while doing the first zooming.

Just after construction:


qDebug()<<m_view->size().height()<<m_view->size().width();
m_scene->setSceneRect(-50,50,m_view->width(),m_view->height());
qDebug()<<m_scene->sceneRect().height()<<m_scene->sceneRect().width();




100 30
100 30


Before doing the first zooming :


qDebug()<<m_view->size().height()<<m_view->size().width();
qDebug()<<m_scene->sceneRect().height()<<m_scene->sceneRect().width();



660 1224
100 30


I would like to have the same coordinate system in the scene and the view. It looks like because of the scene/view size different the zooming is incorrect. Am I right?

Because the first zooming rectangle(from my example zooming) has coordinates:
-285 157
215 157
-285 -143
215 -143
and it's bigger than the scene, so I zoom the whole(the center).
Ok...I belive this is the problem, but yet do not know how to fix it ?

Any help appreciated.


Maverick

wysota
25th August 2007, 23:50
You are creating the scene before the view is first shown. It such situation you can't rely on the view size, thus such small size of the scene. This probably causes the problem - when you zoom enough, the scene will become bigger than the view (after the transformation), causing the zoom to really happen (with wrong parameters, of course).

And that's what I was saying from the very beginning...

BTW. Try doing:

qDebug() << QSize(20, 20);
You don't have to break QSize (and other value based Qt classes) to lowlevel bits like width and height when passing them to qDebug - they'll be printed correctly without that. It simplifies debugging structures very much, you should try it.

maverick_pol
28th August 2007, 08:35
Hi,

Yes I do agree that your hints were correct:

And that's what I was saying from the very beginning...
I see that in my code I have to methods, and firstly I set the scene rect(using mview attributes) and then when I show the view(at my window is maximized view size changes, but not the scene coordinate system).

Thank you for the idea:

You don't have to break QSize (and other value based Qt classes) to lowlevel bits like width and height when passing them to qDebug - they'll be printed correctly without that. It simplifies debugging structures very much, you should try it.

I will test it righ away and try to fix the zooming problem.

Maverick

maverick_pol
28th August 2007, 10:39
Ok the problem is clear, but can't really get what I want.
During construction view size is (30,100). Then after maximixing (660,1224).
I can't set m-scene rectangle as (30,100) because after maximizing the coordinates are wrong. I tried setting the scene rectangle to (660,1224), but than the scene is much bigger than the viewport. My scene should always be the same size as the viewport, have the same coordinates system.

Howto achieve this? I am trying to fix it, but still when zooming is Ok the scene is to big and I have to center is in the view.

Maverick

maverick_pol
29th August 2007, 13:45
Ok, problem solved.

I thank everyone, who helped me, especially wysota.

Maverick

wysota
30th August 2007, 09:31
So what was the solution?