How do I create a rotating cube effect in Qt? - c++

I have a QGraphicsView and a slide show of QGraphicsScenes, at the moment when the user switches to the next slide I just change the Scene that the View is looking at and it changes instantly to reflect that.
What I would like to do it create some transition effects, such as the rotating cube or the slide in/out.
However looking at the QPropertyAnimation class it seems to be about moving an object not transitioning from one to another.
As in I would need a view for each scene and then transition between each view.
What other strategy could I employ?

Instead of changing the scene that the view sees, you could use property animations to slide graphic items in and out of the view from a single scene. That would give you the slide in/out transition without too much effort. The rotating cube effect would be trickier but I think a reasonable facsimile could be produced with property animations.
You could also simulate other effects by subclassing the view widget and adding some custom properties that you could animate and use to direct background or foreground painting.

Related

What is the difference between a Viewport and a View?

Why would you need a viewport? Isn't a View engough? What is the difference?
I have one viewport in my program created so long ago that I forgot it was even there... And now that I want multi-staged rendering and I wonder if I need another one. And the Viewport mdocs page doesn't get into how and why (or maybe I'm blind).
A viewport is how much of the window a view takes up.
A view is what should be visible in that viewport.
A good scenario of having many viewports would be in a splitscreen game.
In a splitscreen game, each player has their own section of the screen. The game can not render one players view inside another players viewport.
Many viewports can also be useful in singleplayer games, like to render a minimap.
Many viewports can also be useful outside of games, like in a text editor. You could have one viewport for the text, and the other for a toolbar.
Think of each window on your computer as being a viewport. You could be in an application that has scrolling, but that is the view moving, not the viewport. When you resize the window, or move the window, you are moving the viewport on your screen - not the view.

How to clear existing content before redrawing QGraphicsItem?

I have a drawing that is built inside a QGraphicsScene with several layers of QGraphicsItem derived objects.
I am repositioning the QGraphicsItem objects based on some parameters and have noticed that there are some "ghost" trails left unless I call graphicsArea->viewport()->update(). I am repositioning the QGraphicsItem objects quite frequently (i.e. when a slider is moved) and updating the viewport only works if I call it manually some time after drawing is finished (e.g. on a button click).
One possible solution that I found was to fill the background of each QGraphicsItem to be a neutral colour. This doesn't work when I have overlapping items though, as the underlying items can get overwritten.
Does anyone have any suggestions?
Thanks,
Alan

Disable drawing items on QGraphicsView

I have two QGraphicsView and one QGraphicsScene. One of views don't need to draw qraphicsitems. Is there any way to disable drawing of items on the view?
From the Qt5.2 documentation:
updatesEnabled : bool
This property holds whether updates are enabled.
An updates enabled widget receives paint events and has a system background; a disabled widget does not. This also implies that calling update() and repaint() has no effect if updates are disabled.
You'll probably have to show some code. There are quite a few ways to do this but it's really based on your implementation.
So one of the most simple ways to not draw items - set flag QGraphicsView::IndirectPaint for graphics view and overload drawItems methods in scene and view. Works for me.

Scaling graphics in Qt

I am writing a scheduling-type application using Qt/C++ and want to display weekly schedules in one part of the window, and have this rendering scale as the window size increases. The renders will be composed of rectangles with text in them, and as the display area increases the rectangles should scale nicely while the text should remain the same size.
I have experimented with QGraphicsScene and QGraphicsView and I can make rectangles and text scale; however, the rectangle scaling seems ugly (stretches the outline) and I don't want text to scale at all.
I suspect that I might want to resize the scene to the display area and re-draw the rectangles and text; however, I am not sure how to do this - QGraphicsScene doesn't seem to respond to resizeEvent. Is this even the right approach?
I'm not sure what the ugly rectangle scaling is about (a screenshot might help me understand better what you meant there), but if you don't want the text parts to scale, you can accomplish that by calling setFlag(ItemIgnoresTransformations, true) on your QTextGraphicItem objects.
As far as automatically rescaling the rectangles in response to a window resize, you might take a look at the documentation of the QGraphicsView::fitInView() method:
Scales the view matrix and scrolls the scroll bars to ensure that the
scene rectangle rect fits inside the viewport [...] It's common to
call fitInView() from inside a reimplementation of resizeEvent(), to
ensure that the whole scene, or parts of the scene, scales
automatically to fit the new size of the viewport as the view is
resized. Note though, that calling fitInView() from inside
resizeEvent() can lead to unwanted resize recursion, if the new
transformation toggles the automatic state of the scrollbars. You can
toggle the scrollbar policies to always on or always off to prevent
this (see horizontalScrollBarPolicy() and verticalScrollBarPolicy()).

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.