PDA

View Full Version : Position of Items in QGraphicsScene/QGraphicsView



StefanK2
6th July 2009, 12:01
Hello,

I have a problem with the position of Items in QGraphicsScenes.

I wrote this simple program for testing, I made the ui with the designer:


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

Form form;

QGraphicsScene scene;

scene.setSceneRect(0, 0, form.graphicsView->width(), form.graphicsView->height());

scene.addRect(0, 0, 10, 10, QPen(), QBrush(QColor(Qt::black)));

form.graphicsView->setScene(&scene);
form.graphicsView->fitInView(scene.sceneRect());

form.show();

return app.exec();
}

When I run this program the RectItem somewhere in the view, not at the position (0,0).

Where is the error in this code, I thought fitInView() manipulates the view in a way that the sceneRect is scaled to the right size.

wysota
6th July 2009, 12:29
You didn't apply a layout on the view. Your view is much smaller than your scene so everything gets scaled down when you try to fit the scene into the view.

StefanK2
6th July 2009, 12:57
Thanks for your fast reply.


You didn't apply a layout on the view. Your view is much smaller than your scene so everything gets scaled down when you try to fit the scene into the view.

I thought I did this with fitInView and by setting the sceneRect to the size of the view?

What do you mean by apply a layout on the view, can't find anything about this.

zgulser
6th July 2009, 13:02
Hi,

I guess you need to add a geometry for the view using setGeometry() method.

wysota
6th July 2009, 13:04
I thought I did this with fitInView
No, I mean the layout of the parent widget of the view.


and by setting the sceneRect to the size of the view?
You have set the size of the scene to the size of the parent widget, not the size of the view.


What do you mean by apply a layout on the view, can't find anything about this.
This is basic Qt stuff. Read about Using layouts in Designer and about Layout classes in general.

StefanK2
6th July 2009, 15:02
Ok, I thought you mean some kind of layout on the graphicsView.

I applied a vertical Layout to the QWidget, I also changed the sceneRect to the size of the Parent-Widget but it still doesn't work.


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

Form form;

QGraphicsScene scene;

scene.setSceneRect(0, 0, form.width(), form.height());

scene.addRect(0, 0, 10, 10, QPen(), QBrush(QColor(Qt::black)));

form.graphicsView->setScene(&scene);
form.graphicsView->fitInView(scene.sceneRect());

form.show();

return app.exec();
}

I attached the ui-file, maybe there is still some kind of error.

wysota
6th July 2009, 15:27
What do you get and what do you expect? By the way, the scene is still of incorrect size. You probably want it the size of the maximumViewportSize().

StefanK2
6th July 2009, 15:43
I try to get the rectangle to position 0,0 on the graphicsview, this is just a test to understand how QGraphicsScene and QGraphicsView work.

I have programmed a version of this without QtDesigner and this works in the way I expected it.


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

QWidget widget;

QGraphicsScene scene;

scene.setSceneRect(0,0,500,500);

scene.addRect(0,0,10,10,QPen(), QBrush(QColor(Qt::black)));

QGraphicsView view(&widget);

view.setScene(&scene);

widget.show();

return app.exec();
}

But when I try to build the gui with QtDesigner than I have this Problem with the size of the QGraphicsScene. I just want that the scene has the same size as the view, so that I can move the Items on the scene and they have the same coordinates on the view.

At the moment I get this window (example2.jpg)

wysota
6th July 2009, 17:11
Don't set the scene size at all (and forget about fitInView). It will automatically adjust to the items it contains.

StefanK2
6th July 2009, 21:50
Ok thanks,

I will try this.

StefanK2
7th July 2009, 12:57
I used maximumViewportSize(), this worked.

When I don't set a sceneRect, the position 0,0 is the center of the view.

Thanks for your help.

wysota
7th July 2009, 14:04
When I don't set a sceneRect, the position 0,0 is the center of the view.

The whole scene is centered in the view. Unless you place an item in negative coordinates of the scene, the scene's left top corner will have (0,0) coordinates.