Moving from QGLWidget to QWindow - c++

I have some code using Qt 4. I want to migrate it to Qt 5 and switch to QOpenGL stuff. I can't understand where the code from void paintGL() and void initializeGL() needs to go in a QWindow. Can anyone help me with an example?

I have created a simple example of using QWindow with OpenGL.
To simplify OpenGL development I have created an abstract class GLWindow, which contains the virtual functions initializeGL(), paintGL() and resizeGL(int w, int h). I believe the source code shows the relationship between the old style and the new style.
The example is available here:
https://github.com/mortennobel/QtOpenGLChapter/tree/master/OpenGL3xAlt

Qt has at least one example (Overpainting) of putting widgets over an OpenGL scene. By looking at that, it appears the easiest way would be to inherit from QGLWidget instead of QWidget and override the appropriate virtual functions for initialize and paint.

The standard example for using QWindow with OpenGL is hellowindow in qtbase/examples/opengl.
There is no direct replacement for initializeGL and paintGL. Instead you do something like this:
Have a QWindow with surface type OpenGLSurface.
Create a QOpenGLContext with a matching format.
When the window receives an expose event, start rendering (makeCurrent, your GL calls and finally swapBuffers).

Related

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

QOpenglWidget and QML Quick

Which is the best or right way to implement a QML overlay over an existing QOpenglWidget? QDeclarativeView is deprecated and works only with QtQuick 1.0. This is a general question for a widget use case.
This might be too late for you, but I was able to make a QOpenGLWidget as an underlay. This pointer from KDAB got me started. Basically, You have to make some public wrappers around the protected QOpenGLWidget functions, initializeGL() and paintGL() for the QQuickView sceneGraphInitialized and beforeRendering signals.

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.

Render loop function in QT to render opengl in Windows

Since QGLWidget cannot be used for OpenGL / ES, i am left with QWidget in Qt for widgets to render opengl. Reference
if i have the following renderView function to render in the current window, i am looking for "that" function that will be called repeatedly by qt for QWidget to refresh screen so that i can call renderView in that function by overloading it.
any idea how to do this or common work around?
Original problem: I do not want to call renderView in a timer of 1 MilliSec or even 16.66 Milli sec as per the screen refresh rate.
class MyWindow
{
void renderView()
{
if(DeviceContext && RenderingContext)
{
wglMakeCurrent(m_hdc, m_hrc); //Set current context
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glViewPort.... and render using shaders
SwapBuffers(m_hdc); // Double buffering thats why
}
}
};
class MyWidget : public QWidget, public MyWindow
{
// What is the function called repeatedly to refresh the screen
// so that i would ask that function to call renderView
};
Thanks in advance!
Since QGLWidget cannot be used for OpenGL / ES
Right, we need to clear up some confusion before going further down the road...
It can be. This widget was invented to extend the usual QWidget interface with OpenGL desktop and embedded support. Please refer to the following example to see yourself it is possible to use this widget for OpenGL ES functionality:
OpenGL Hello GL ES Example
Now, we can come to the point to answer your question. What you should probably reimplement is the following protected method.
void QGLWidget::paintGL() [virtual protected]
This virtual function is called whenever the widget needs to be painted. Reimplement it in a subclass.
While we are at discussing it, please also check the protected initialization method:
void QGLWidget::initializeGL() [virtual protected]
This virtual function is called once before the first call to paintGL() or resizeGL(), and then once whenever the widget has been assigned a new QGLContext. Reimplement it in a subclass.
This function should set up any required OpenGL context rendering flags, defining display lists, etc.
In fact, you could even deal with overlays with this interface as per the following protected methods:
void QGLWidget::paintOverlayGL() [virtual protected]
This virtual function is used in the same manner as paintGL() except that it operates on the widget's overlay context instead of the widget's main context. This means that paintOverlayGL() is called whenever the widget's overlay needs to be painted. Reimplement it in a subclass.
and
void QGLWidget::initializeOverlayGL() [virtual protected]
This virtual function is used in the same manner as initializeGL() except that it operates on the widget's overlay context instead of the widget's main context. This means that initializeOverlayGL() is called once before the first call to paintOverlayGL() or resizeOverlayGL(). Reimplement it in a subclass.
This function should set up any required OpenGL context rendering flags, defining display lists, etc. for the overlay context.

Seeking advice on using QGLWidget in Qt4

I'm new here, and have a question about opengl in Qt4, which I've been learning over the last few months.
Particularly, I'm seeking advice on the best way to compose a scene in a good object-oriented fashion using the QGLWidget. I'd ideally like every item in my scene to be sub-classes of a super 'Entity' class. Then in my main QGLWidget I can sort the entities and render them accordingly.
I noticed though that certain openGL functions (like bindTexture) need to be called from the QGLWidget (or the widget's QGLContext). At the moment I'm passing a pointer to the QGLWidget that controls my main viewport to each entity and storing it so that I can gain access to those functions. Is this a good idea?
Any advice would be gratefully received, or even directions to good websites/books that might be of help. I've got the Blanchette/ Summerfield book but the OpenGL section is quite short and most of the examples on the Qt website are pretty simplistic.
Thanks,
Dan
I agree with Vime: You're building a scene graph, and there are a number of classical approaches for designing its object hierarchy. Check out "3D Game Engine Design," by Dave Eberly, for details on one such engine, and look at OGRE for another example.
Since only one GL context can be active at a time on a particular thread, consider storing the QGLWidget pointer as a static class member to save effort:
class MyGLWidget : public QGLWidget {
// ...
public:
static inline MyGLWidget *GetActiveWidget() {
return ms_activeWidget;
}
protected:
static __declspec(thread) MyGLWidget *ms_activeWidget = 0; // uses MSVC extension
inline void SetActiveWidget() {
ms_activeWidget = this;
}
};
void MyGLWidget::paintGL() {
SetActiveWidget();
// ...
}
Then in your entity classes you can simply call MyGLWidget::GetActiveWidget() on the few occasions when you need to call QGLWidget member functions, and not need to copy a (probably invariant) pointer all over the place.
You are building something that is usually called "scene graph". In our engine scene graph objects need no access to QGLWidget. It's enough if you create your OpenGL objects inside void initializeGL() and render everything inside void paintGL().
There is a slightly higher level Qt/OpenGL example called Boxes. It can be found from qt/demos/boxes folder in Qt 4.6 installation.