How to animate some property of custom `QQuickItem` based class using C++? - c++

As we know, Scene Graph has special multithreaded v-sync aware render technology.
Also QtQuick has special C++ classes specified with QSG* prefix names and are strongly recommended to be used only those classes inside QQuickItem.
So is it ok to use regular (old?) C++ Animation Framework classes such as QPropertyAnimation in QQuickItem classes?
How I can animate some properties inside QQuickItem using C++ without breaking Scene Graph performance/rules?
Thank you

Related

Differences between QQuickFramebufferObject and QQuickItem?

I am working on an application where I want to embed openGL scenes into a QML item. I want my custom QML items to be driven by C++, and this article gives an overview of how to do so with a QQuickFramebufferObject.
However, I've noticed that very similar things can be done by simply using a QQuickItem, the distinction seeming to be that QQuickItem has different methods (paint()) compared to the QQuickFrameBufferObject (synchronize(), render(), createFramebufferObject()).
So what are the advantages of one over the other? Are these compatible? What is the best way to render a scene with these options? I've heard that the QQuickFrameBufferObject provides more flexibility with using custom openGL, but that means relatively little to me.

Integrate GUI with OpenGL in C++

I need to create a GUI with a file menu and menu in which the user can input parameters. The parameters are then used for drawing rectangles in a canvas which is part of the application window. Is there a way to scale the OpenGL subwindow to just one part of the screen and the parameter input to the other? The application needs to be written in C++.
Is it possible to create a GUI with QT and draw the rectangles in the same window using OpenGL? If not, what is the common way to integrate a GUI with OpenGL? (or any other graphics library which I can use to draw rectangles from points as easy as possible)
EDIT: I am not sure If OpenGL is necessary or there is a way to paint the rectangles on the canvas like you can in Java with paintComponent().
I have never used QT before so I am not aware of its capabilites.
you can use opengl window singly or use this in the common mainwindow. previous example (in first answer) show how to use opengl window in qt singly and without communication with other components of Qt (like menu, toolbar and ...). but you can add a opengl window to a mainwindow (like other widget) and use it alongside other widgets . this example can help you.
Yes, using OpenGL together with Qt is absolutely possible. There is even an example for that and Qt provides classes for a more object oriented way of using OpenGL. Have a look here (Section "OpenGL and OpenGL ES Integration") for more details.

Qt QML draw in cpp subclass

so I am trying to understand drawing in Qt, but I dont get this one:
So first of all I had a qml file where I embedded a custom class called Game which has this constructor: Game::Game(QQuickItem *parent) : QQuickItem(parent)
Writing setFlag(ItemHasContents, true); in the constructor code was sufficient to being able to draw with QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *); using QSGGeometry (This worked). Now I tried to draw in a subclass, using the same procedure but now it does not work anymore: So what I did was create a class called qcurver, and in Game I create an instance of qcurver. QCurver has the same constructor like Game and I pass the parent of Game so that technically it has the same parent as Game, but now when I try to draw in this class, nothing happens. Does anyone know, how I can pass the QML object for drawing, so that I actually see the drawing?
You're creating a Game object from QML code, so it's registered correctly in the QML scene. But the QCurver isn't created there, so it won't work.
Yes, it has the same parent, but it only means that if the parent gets deleted - the QCurver will be deleted too. Qt doesn't revise all QObjects that are added as children to find out if they are of type QQuickItem and should be rendered.
Also, QML itself was added to be able to avoid doing widget hierarchies in C++. So, compose hierarchies in QML. Or, if you want some of them in C++ for the performance reasons - then it is possible to do QSGNode hierarchies inside a QQuickItem (that's useful for rendering complex scenes with OpenGL).

What Qt classess use to make an animation?

I wrote almost every sort algorithm in C++ in console. Now I would like to make it look good, so what classes of Qt should I use to make an animation of sorting algorithms I implemented in pure C++? Im a newbie in Qt ;) tia
You can use QGraphicsItemAnimation to animate graphics displayed using QGraphicsScene and QGraphicsView.
Links:
Graphics View Framework
QGraphicsItemAnimation
Outside of the Graphics View Framework, you can use QVariantAnimation or QPropertyAnimation to drive properties or methods of any QObject derived class. See the Animation Framework docs for more info.
Conversely you can manually configure a QTimer to drive painting updates of a widget and use an increment to drive a QTimeLine.

Hosting QOpenGL widget inside QML

I have a library proving me a QGLWidget, and the interface allow me to only to resize/set size, and control some GL animation; but no GL command is exposed outside, all i do it initialize GLWidget, and then pass the context to library and later call swap buffer to show animation..
I want to integrate this QGLWidget library into QML, is it possible to hove a QGLWidget inside QML ? if yes how ?
It's totally possible! You can write a QML plugin that will define a new QML element to encapsulate the library.
Then you will import this plugin from the QML document and you'll be good to use the new element and harness the features that the library offers.
Tip: if the application that loads your QML document was setup to have it's on QGLWidget, then you won't need to create a new QGLWidget inside your plugin. I did this mistake once.
This blog post shows how to create a simple/new QML element from scratch and how to use it in a QML document.
QGLWidget derives from the QWidget while QML widgets are implemented as QDeclarativeItem which derives from QGraphicsObject and these two are to different worlds.
Possible way of doing OpenGL drawings in a QML item is to declare a new QDeclarativeItem, expose it to the QML system and then override the draw method of this QDeclarativeItem subclass to do native painting(by calling the beginNativePainting and endNativePainting of the QPainter instance provided in the draw method).
Have a look at these two links:
http://doc.qt.nokia.com/4.7-snapshot/qml-extending.html
http://developer.qt.nokia.com/forums/viewthread/4109