Results 1 to 11 of 11

Thread: Draw with OPenGL inside QMAinWindow

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Draw with OPenGL inside QMAinWindow

    Quote Originally Posted by zulunation View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw with OPenGL inside QMAinWindow

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Draw with OPenGL inside QMAinWindow

    Quote Originally Posted by Radek View Post
    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Sep 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Draw with OPenGL inside QMAinWindow

    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?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Draw with OPenGL inside QMAinWindow

    Quote Originally Posted by zulunation View Post
    Why does that happen?
    Because you are not calling makeCurrent()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Draw with OPenGL inside QMAinWindow

    I do.
    Here is my paintGL function

    Qt Code:
    1. void paintGL()
    2. {
    3. makeCurrent();
    4. glViewport(0,0,500,500);
    5.  
    6. glMatrixMode(GL_PROJECTION);
    7. glLoadIdentity();
    8. glOrtho(0,500,0,500,0,-100);
    9. //glOrtho(-1,1,1,1,0,-1);
    10.  
    11. glMatrixMode(GL_MODELVIEW);
    12.  
    13. glLoadIdentity();
    14. glClear(GL_COLOR_BUFFER_BIT);
    15. glClearColor(0,1,0,1);
    16. glColor3f(1,0,0);
    17.  
    18. const int x = 100;
    19. int y = 100;
    20.  
    21. const int dx = 26;
    22. const int dy = 16;
    23.  
    24. glBegin(GL_LINES);
    25. for(int i = 0;i<10;++i)
    26. {
    27. glVertex3f(x,y,0);
    28. glVertex3f(x+dx,y,0);
    29.  
    30. glVertex3f(x+dx,y,0);
    31. glVertex3f(x+dx,y+dy,0);
    32.  
    33. glVertex3f(x+dx,y+dy,0);
    34. glVertex3f(x,y+dy,0);
    35.  
    36. glVertex3f(x,y+dy,0);
    37. glVertex3f(x,y,0);
    38.  
    39. y+=18;
    40. }
    41. //glVertex3f(90,90,0);
    42. glEnd();
    43.  
    44. swapBuffers();
    45. }
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. how to draw custom titlebar for QMainWindow ... ?
    By pradeepreddyg95 in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2017, 09:23
  2. Access widget inside the QMainWindow centralwidget
    By graciano in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2014, 21:40
  3. How to open an external App inside QMainWindow
    By lorik in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2011, 01:07
  4. QMainWindow inside another widget
    By DanFessler in forum Newbie
    Replies: 4
    Last Post: 23rd August 2010, 21:58
  5. switch a QWidget inside a QMainWindow to fullscreen
    By koenux in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2009, 21:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.