QGraphicsScene disable setting right and bottom margin - c++

I fit my scene for view's size, but it's content has some margins on right and bottom sides.
m_ui.graphicsView->fitInView(m_scene->sceneRect(), Qt::IgnoreAspectRatio);
Can I avoid this behavior?

I think you're misunderstanding the point of the fitInView function. it's there to ensure that the given rect area of the scene is visible in the view. As the docs state: -
Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport. rect must be inside the scene rect; otherwise, fitInView() cannot guarantee that the whole rect is visible.

Related

How to clip the corner rectangle created by two scrollbar controls

Let's say you have a resizable window with child scrollbar controls, and the scrollbars come and go depending on whether the window contents are large enough to require scrolling.
When both scrollbars are present, a small rectangle is effectively created in the bottom right corner of the window, at their intersection. Is there a clean strategy for clipping that rectangle when drawing on the window, so that you don't paint on it?
I guess my current approach is to obtain the rectangles for each scrollbar, and if those rectangles are not null, then use the rectangles' locations to determine the rectangle that we want to clip. And then call ExcludeClipRect for that rectangle. I guess a similar approach could be used, except with GetSystemMetrics(SM_CXVSCROLL) and GetSystemMetrics(SM_CYVSCROLL) to get the rectangle dimensions.
But is there a more accepted way of doing this, perhaps using some helpful clipping API functions? Thank you for any input.

QGraphicsView and QGraphicsScene coordinate systems

in Qt 4.8 i have create a QGraphicsView and a DynamicRadarScene(derived from QGraphicsScene):
QGraphicsView* view = new QGraphicsView;
view->setMinimumSize(800, 600);
DynamicRadarScene* _scene = new DynamicRadarScene(mode, channel_types, this);
view->setScene(_scene);
What is the coordinate system of QGraphicsScene? (0,0) is from upper left corner?
How can i draw an item in the upper right corner of the scene (i have set it 800x600: view->setMinimumSize(800, 600);)?
If i resize the widget and so i resize the QGraphicsView, how can move the item i have drawn before to remain in the upper left corner?
Yes, the upper left corner is generally the coordinate of (0,0) in a graphics scene. What you need to consider is that a QGraphicsView is like a window looking into a world (the QGraphicsScene). You set the view to look at an area or the scene, which may be all or just part of the scene.
Once the scene and view are setup, you can then add QGraphicsItems / QGraphicsObjects or instances of classes derived from those by using functions such as QGraphicsScene::addItem. These items are positioned in the scene and draw themselves.
i (sic) resize the widget and so i resize the QGraphicsView
You can change the QGraphicsView position and dimensions, but then the items in the scene will remain in the same place within the scene. Usually you would set up the scene and view and then move / resize the graphics items / objects within the scene, often with the setPos function: QGraphicsItem::setPos(). This sets the position of the item, relative to its parent. If the item has no parent, it sets the position of the item in the scene.
QGraphicsScene has property sceneRect. If it is not set then it is auto adjusted depending on scene content. This can give a filling that origin of coordinating is in some strange place or even that it is mobile.
Set this property. See also this.

QGraphicsView viewport rect problems

When I set the scene rect to the same rect of the viewport:
mCanvasScene->setSceneRect(mCanvasView->viewport()->rect());
The QGraphicsView creates scrollbars in the widget, but if the scene is the same size of the viewport why there are scrollbars ? It seems that the rect() from the viewport isn’t the correct size of the viewport area.
Does anyone knows how to set the scene with the same size of the visible area of the viewport ?
QGraphicsView inherits from QAbstractScrollArea, and viewport() returns widget managed by scroll area, which is usually bigger than the actual scroll area widget (reason for using scroll area in the first place).
Correct size of the visible area is the size of QGraphicsView (calling the inner widget viewport is little misleading).

Why do the first added item always appear at the center in a graphics scene/view in Qt?

I have a graphics scene and view, and then I'm adding a single element. But no matter what x/y coordinate I give, it always appears at the center of the graphics view.
Why does this happen, and how can I make it appear at, say, the upper left corner?
This is my code:
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QGraphicsEllipseItem *ellipseItem = scene->addEllipse(150, 150, 10, 10);
The reason for this is that by default, QGraphicsScene computes its sceneRect by adding all the item rectangles together. When you add the first item, it automatically uses it as the scene rect. And by default QGraphicsView scales and centers on the scene rect.
If you know the final or desired scene rect, set it before you add any item:
scene->setSceneRect(0, 0, 800, 600);
scene->addEllipse(150, 150, 10, 10);
You probably want to define a scene-rectangle that is shown by the QGraphicsView. I think the default view just shows the centered bounding rectangle of the current scene, which is just your ellipse. You can use QGraphicsView::fitInView to define the area to be shown explicitly.

Avoid automatic transition of QGraphicsScene on QGraphicsView

Qt's QGraphicsView has a habit of automatic transition/sliding of the view in such a way that everything drawn on the attached QGraphicsScene is visible. For example, suppose you've drawn something on the scene and viewing in the view. Now upon a button press you draw something along the right edge of the view. The scene/view will slide to the right slightly so that everything is again visible.
I want to prevent this to happen. How can I do that?
Thanks.
The scene rectangle of QGraphicsScene is by default the bounding rectangle of all the items in the scene. As such it grows when items are added.
The scene rectangle of QGraphicsView is by default taken from QGraphicsScene::sceneRect. If you don't want this to change automatically you can set one explicitly with QGraphicsView::setSceneRect.