PDA

View Full Version : Draw with OPenGL inside QMAinWindow



zulunation
5th November 2014, 13:47
HI al,

I want to draw with openGL inside QMainWindow. I have looked all examples and QWindow was used there.
The problem is that i don't know how to get QSurface from the QMainWindow.
I need it to call swapbuffers makecurrent functions of the context.
It is easy done in QWindow but not it QMainWindow.
I don't want to use QGLWidget.
Do i need to create additiona QWindow upon the QMainWindow?
Thanks,

wysota
5th November 2014, 14:44
Why you don't want to use QGLWidget?

zulunation
5th November 2014, 15:45
>>Why you don't want to use QGLWidget?

I don't want to create an additional window. I have QMainWindow created and to draw inside it's client are.
I think that QGLWidget is an additional window which is created upon the parent's window.
Is it true or it is just a wrapper?

wysota
5th November 2014, 17:25
>>Why you don't want to use QGLWidget?

I don't want to create an additional window. I have QMainWindow created and to draw inside it's client are.
In theory since Qt 5.4 (which is not yet released) this is possible however this would require that all widgets inside the window are also drawn using OpenGL which in general is not a good idea.


I think that QGLWidget is an additional window which is created upon the parent's window.
So? What problem is that? Since Qt 5.4 (yeah, the one not released yet) you can use QOpenGLWidget which does not create an additional invisible window. QGLWidget will become deprecated then.


Is it true or it is just a wrapper?
A wrapper over what?

zulunation
5th November 2014, 17:46
I was programming GUI using WinAPI for a long time. Recently switched to Qt.
Remember if we have a window in Win system we can get it device context and draw inside any window.
Here in Qt we are creating additional window. That is a bit strange to me.
I was thinking that QGLWidget is just a wrapper which encapsulates access to the QMainWindow device context.

wysota
5th November 2014, 19:45
Remember if we have a window in Win system we can get it device context and draw inside any window.
I don't see how that's relevant.


Here in Qt we are creating additional window. That is a bit strange to me.
"In Qt we have a backing store which does double buffering for us by default. In WinAPI we get flicker instead. That is a bit strange to me".


I was thinking that QGLWidget is just a wrapper which encapsulates access to the QMainWindow device context.
Regular widgets are raster based, they don't have a GL context (at least until Qt 5.4). This is really nothing you should worry about in normal conditions.

Radek
6th November 2014, 06:20
QGLWidget need not be a "new window". It's a widget. Period. Therefore, create a main window, create a widget as a part of its central widget, and promote the widget to a QGLWidget - or rather to a class derived from the QGLWidget. You have an OpenGL window as a part of your main window. The OpenGL window can be the whole client area of the main window or you can create additional control widgets around.

Now, use OpenGL for drawing in your OpenGL winwow.

wysota
6th November 2014, 09:56
QGLWidget need not be a "new window". It's a widget. Period.
Unfortunately it is not so simple. QGLWidget generates an invisible window (or even a couple of windows on Windows) in the window system. So logically (from the program point of view) there is no additional window (as in something you can drag around) but physically using QGLWidget requires a native window handle which forces the parent widget to also have a native window handle and so on.

But from programmer's point of view there is nothing to worry about in normal conditions. Before Qt 5.4 there is nothing one can do about it anyway (well, you can render to FBO, convert that to image and then draw the image on the widget but that has its downsides too).

zulunation
11th November 2014, 18:27
I have successfully set up Opengl on a QGLWidget. It really creates additional window on Windows OS.
That's ok for me.
There is one thing i can't understand.
I'm drawing in the paintGL function. I do not call makeCurrent() swapBuffers() functions at the beginning and at the end respectively.
Everything is fine. When i do that i get:

QOpenGLContext::swapBuffers() called without corresponding makeCurrent()

message in the debugger output window when i call swapBuffers().
Why does that happen?

wysota
11th November 2014, 18:59
Why does that happen?

Because you are not calling makeCurrent() :)

zulunation
12th November 2014, 11:35
I do.
Here is my paintGL function



void paintGL()
{
makeCurrent();
glViewport(0,0,500,500);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,500,0,500,0,-100);
//glOrtho(-1,1,1,1,0,-1);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0,1,0,1);
glColor3f(1,0,0);

const int x = 100;
int y = 100;

const int dx = 26;
const int dy = 16;

glBegin(GL_LINES);
for(int i = 0;i<10;++i)
{
glVertex3f(x,y,0);
glVertex3f(x+dx,y,0);

glVertex3f(x+dx,y,0);
glVertex3f(x+dx,y+dy,0);

glVertex3f(x+dx,y+dy,0);
glVertex3f(x,y+dy,0);

glVertex3f(x,y+dy,0);
glVertex3f(x,y,0);

y+=18;
}
//glVertex3f(90,90,0);
glEnd();

swapBuffers();
}


I assume makeCurrent/SwapBuffers are not necessary if we are dealing with one widget. They are called by paintGL implicitly. But not sure about that.