Hosting QOpenGL widget inside QML - opengl

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

Related

How to animate some property of custom `QQuickItem` based class using 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

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

Opening Qt window from QML

I have created a C++ Qt class, inheriting from QWidget. I have also created a QML file, which runs as my main, and I want in some point of the program to open another separated window containing this widget.
The point is to draw line graphs in QML, and I don't quite understand how to do it.
First of all, take a look at this question and its answers: Qt5. Embed QWidget object in QML
You got into XY-problem. You do not need to embed QWidget to your QtQuick/QML application. (I hope) You can easily deal with your task using only QML. For example, there is Canvas Element that can help you to organize drawing. Take a look at tutorial related to Canvas in QmlBook.
Hope this helps!

Can QOpenGLWidget be integrated with third party OpenGL library?

Legacy QGLWidget could be integrated with rendering libraries (like SFML) by passing winId() result to the rendering library. But I can't make QOpenGLWidget work that way. After making it MainWindow's central widget I get series of warnings like QOpenGLWidget cannot be used as a native child widget. Consider setting Qt::AA_DontCreateNativeWidgetAncestors and Siblings.. Furthermore, the documentation says "QGLWidget on the other hand uses a native window and surface. (...) QOpenGLWidget avoids this by not creating a separate native window.". Can QOpenGLWidget be integrated with third party OpenGL software at all, or is it unsupported now?
You need to create a QWindow, initialize it and integrate into application with QWidget::createWindowContainer
class MyNativeWindow : QWindow
{
MyNativeWindow() : QWindow
{
setSurfaceType(QWindow::OpenGLSurface);
}
};
MyNativeWindow *nativeW = new MyNativeWindow();
QWidget *w = QWidget::createWindowContainer( nativeW );
// Use w as a simple QWidget
In some cases you don't need to use winId to get HWND. It is enough to know OpenGL context id. For custom gl context manipulation you may use QOpenGLContext class.
Be careful, because if your third party libraries will create native windows (in OS X) by themselves, you will have a lot of bugs with Qt. We are tied to fix bugs in our project. (Undockable docks, keyboard focus lost, impossibility of opening menus, errors with fullscreen etc.)
You may look at this code sample. And a custom context code sample.

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.