QGraphicsView viewport rect problems - c++

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).

Related

QTabWidget with QGraphicsView

I have a QTabWidget and second page containing a QGraphicsView with scene. The scene canvas is larger than the QGraphicsView's viewport and I'm using QScrollBar to move objects on the scene.
On the scene I place a lot of QGraphicsItem objects. I have a horizontal and a vertical scroll bar, and some items can move only horizontally, another objects on scene can move only vertically.
My QGraphicsview coordinates start at the top left corner of QGraphicsview. After moving objects on the scene, if I change the page of the QTabWidget from second page to first and then back again, the start of coordinates is moved to the center of QGraphicsView, and all objects on the scene are moved to new positions automatically.
This happens only when I'm using a QTabWidget, if I change the parent of QGraphicsView to QWidget, all works well.
I found a solution. Just need to calculate rect of the scene, and set obviously using QGraphicsScene::setSceneRect() method. Otherwise, position of my scene in the QGraphicsView may surprise.

Get size of QScrollArea viewport before showing

I have a custom QDialog comprised of a QStackedWidget with QScrollArea widgets for each page of the stacked widget.
I want to set the size hint for the QDialog such that the dialog is just large enough that the scroll bars for the scroll area are not visible when the dialog is first shown (i.e. ensure size of QScrollArea viewport = size hint of child widget in scroll area). Currently, the default sizeHint() implementation for the QDialog has insufficient height, which causes the vertical scroll bar to be shown when first loaded.
I thought this could be achieved by re-implementing sizeHint() for the QDialog, whereby the size hint of the dialog would be adjusted by the amount required for the size of QScrollArea viewport to equal the size hint for child widget in the scroll area (for the first page of the stacked layout). Unfortunately, in sizeHint(), the size of the QScrollArea viewport is set to the default size of QStackedWidget (640x480), and only updates to the correct size once the QDialog is shown.
Is there some way to get the correct size of the QScrollArea viewport before it is shown, or another way to achieve the desired effect of adjusting the size hint of the dialog to prevent scroll bars from being shown when it is first displayed (aside from hard-coding the dialog size).
With the composition of your dialog as:
I have a custom QDialog comprised of a QStackedWidget with QScrollArea
widgets for each page of the stacked widget.
The tricky part is to answer:
Is there some way to get the correct size of the QScrollArea viewport
before it is shown?
Well, before switching to certain page you can estimate the scroll area viewport if it is either correctly set or you can just measure the content going inside the scrollarea. I usually force the widget to demand certain height from the scroll area like that:
wdgetInScrollArea->setMinimumSize( widgetInScrollArea->sizeHint() );
wdgetInScrollArea->adjustSize(); // sometimes it is needed
The the scroll area viewport hint is then more 'adequate':
qDebug() << scrollArea->viewPortSizeHint(); // report
I don't see the code but usually it is not even required to do any custom event handling here, just prepare all the nested widgets like that.

scrolling graphicsWidget using mouse

im adding QGraphicsLayoutItem in a QGraphicsWidget in run time using QGraphicsLinearLayout.
i have to add more items to the layout that exceeds the height of the viewport height and items shown outside the viewport area.so is it possible to pan or scroll in QGraphicsWidget like QGraphicsView. QGraphicsView i can pan using centerOn and showing only a visible rect.
the normal view
and when more item added

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.

QGraphicsScene disable setting right and bottom margin

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.