1 Attachment(s)
Position of Items in QGraphicsScene/QGraphicsView
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:
Code:
int main(int argc, char *argv[])
{
Form form;
scene.setSceneRect(0, 0, form.graphicsView->width(), form.graphicsView->height());
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.
Re: Position of Items in QGraphicsScene/QGraphicsView
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.
Re: Position of Items in QGraphicsScene/QGraphicsView
Thanks for your fast reply.
Quote:
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.
Re: Position of Items in QGraphicsScene/QGraphicsView
Hi,
I guess you need to add a geometry for the view using setGeometry() method.
Re: Position of Items in QGraphicsScene/QGraphicsView
Quote:
Originally Posted by
StefanK2
I thought I did this with fitInView
No, I mean the layout of the parent widget of the view.
Quote:
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.
Quote:
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.
1 Attachment(s)
Re: Position of Items in QGraphicsScene/QGraphicsView
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.
Code:
int main(int argc, char *argv[])
{
Form form;
scene.setSceneRect(0, 0, form.width(), form.height());
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.
Re: Position of Items in QGraphicsScene/QGraphicsView
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().
1 Attachment(s)
Re: Position of Items in QGraphicsScene/QGraphicsView
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.
Code:
int main(int argc, char *argv[])
{
scene.setSceneRect(0,0,500,500);
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)
Re: Position of Items in QGraphicsScene/QGraphicsView
Don't set the scene size at all (and forget about fitInView). It will automatically adjust to the items it contains.
Re: Position of Items in QGraphicsScene/QGraphicsView
Ok thanks,
I will try this.
Re: Position of Items in QGraphicsScene/QGraphicsView
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.
Re: Position of Items in QGraphicsScene/QGraphicsView
Quote:
Originally Posted by
StefanK2
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.