Results 1 to 2 of 2

Thread: Update QGL Widget on different Thread

  1. #1
    Join Date
    May 2017
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Update QGL Widget on different Thread

    Hi,

    Here is my problem, I'm trying to update the QGLWidget from an other thread. But when i try i got this warning:
    "QOpenGLContext::swapBuffers() called without corresponding makeCurrent()"

    I want to retrieve a video stream from a camera and display it on a QGLWidget. With the NVAPI, i fill one image in a GLTexture each frame. That's what i want to display. (and i have displayed it without update it in a thread)
    But as i need to update it continously for the stream, i want to do it in a stream.

    Here is my code:

    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QGLWidget *parent) :
    5. QGLWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Widget::~Widget()
    12. {
    13.  
    14. nv.cleanupVideo();
    15. th.join();
    16. delete ui;
    17. }
    18.  
    19. void Widget::initializeGL(){
    20. glewInit();
    21. //Initialize the capture of the camera
    22. nv = NVConfig();
    23. this->contextGL = wglGetCurrentContext();
    24. nv.GlobalInit();
    25. nv.LockDevice(GetDC((HWND)this->winId()),this->contextGL);
    26. //Unbind the OpenGL context from this thread.
    27. wglMakeCurrent(0, 0);
    28. //Launch the capture on an other thread
    29. th = std::thread(&Widget::updateGL,this);
    30. }
    31.  
    32. void Widget::resizeGL(int w, int h){
    33. if(h==0)
    34. h =1;
    35. glViewport(0,0,w,h);
    36. glMatrixMode(GL_PROJECTION);
    37. glLoadIdentity();
    38. perspectiveGL(45.0f,w/h,0.1f,100.0f);
    39. glMatrixMode(GL_MODELVIEW);
    40. glLoadIdentity();
    41.  
    42. }
    43.  
    44. /*
    45. * The capture will fill a GL Texture 1280*720; GL_RGBA
    46. *
    47. */
    48. void Widget::updateGL(){
    49. //Start the capture, the previous OpenGL is bind again with
    50. // wglMakeCurrent(GetDC((HWND)this->winId()),this->contextGL)
    51. nv.StartCapture(GetDC((HWND)this->winId()),this->contextGL);
    52. }
    53.  
    54. void Widget::paintGL(){
    55. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    56. glLoadIdentity();
    57. glTranslatef(-1.5f,0.0f,-6.0f);
    58. glBegin(GL_QUADS);
    59. glTexCoord2f(0,1.0f);
    60. glVertex3f(0,0,0);
    61. glTexCoord2f(0,0);
    62. glVertex3f(0,1.0f,0);
    63. glTexCoord2f(1.0f,0);
    64. glVertex3f(1.0f,1.0f,0);
    65. glTexCoord2f(1.0f,1.0f);
    66. glVertex3f(1.0f,0,0);
    67. glEnd();
    68.  
    69.  
    70. }
    71.  
    72. void Widget::perspectiveGL(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar){
    73. const GLdouble pi = 3.1415926535897932384626433832795;
    74. GLdouble fW, fH;
    75. fH = tan( fovY / 360 * pi) * zNear;
    76. fW = fH * aspect;
    77. glFrustum( -fW,fW,-fH,fH,zNear,zFar);
    78. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Update QGL Widget on different Thread

    You need to make the GL context current to bind it to the thread. When you're done, you can release the context with a corresponding call.
    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.


  3. The following user says thank you to wysota for this useful post:

    miyoku157 (9th June 2017)

Similar Threads

  1. Replies: 51
    Last Post: 21st April 2015, 12:12
  2. Update GUI in a thread
    By olivier1978 in forum Qt Programming
    Replies: 3
    Last Post: 8th January 2011, 21:33
  3. Update GUI from another thread
    By Anne in forum Qt Programming
    Replies: 9
    Last Post: 14th July 2010, 15:08
  4. update widget from separate thread
    By method in forum Qt Programming
    Replies: 5
    Last Post: 10th July 2009, 14:33

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.