I would like to connect QGLWidget widget in Qt to the Main Window UI. Basically, I would like to send a signal to the QGLWidget when user clicks on a button. I have defined a signal and a slot in my MainWindow and GLWidget as follows:

**Signal**


Qt Code:
  1. class MainWindow : public QMainWindow
  2. {
  3. Q_OBJECT
  4. ...
  5. signals:
  6. void draw(void);
  7. ...
To copy to clipboard, switch view to plain text mode 

**Slot**

Qt Code:
  1. class GLWidget : public QGLWidget
  2. {
  3. Q_OBJECT
  4. ..
  5. public slots:
  6. void updateScreen(void);
  7. ...
To copy to clipboard, switch view to plain text mode 
and in MainWindow.cpp
I do



Qt Code:
  1. void MainWindow::on_drawButton_clicked()
  2. {
  3. //do stuff to draw function in paintGL
  4. emit draw();
  5. qDebug()<<"draw emmited";
  6. ..
To copy to clipboard, switch view to plain text mode 


I connect them in MainWindows Constructor as follows..

Qt Code:
  1. GLWidget *g = new GLWidget;
  2. connect(this, SIGNAL(draw()), g, SLOT(updateScreen()));
To copy to clipboard, switch view to plain text mode 

updateScreen() has a message to let me know if the slot is executed. I tried updateGL() and update()

Qt Code:
  1. void GLWidget::updateScreen(void)
  2. {
  3. qDebug()<<"I'm Updating!";
  4. updateGL();
  5. update();
  6. }
To copy to clipboard, switch view to plain text mode 

The slot is being executed, but the screen is not being updated to reflect the new things I have drawn on the screen

**The screen only update when I resizeGL is called from actions**

I use a temporary fix using a timer..

Qt Code:
  1. //temporary fix
  2. QTimer *timer = new QTimer(this);
  3. connect(timer, SIGNAL(timeout()), this, SLOT(updateScreen()));
  4. timer->start(1000);
To copy to clipboard, switch view to plain text mode 
My GLWidget.cpp file is below..

Qt Code:
  1. ...
  2.  
  3. #include "glwidget.h"
  4. #include "motion.h"
  5. #include "MainWindow.h"
  6. #include <GL/glut.h>
  7.  
  8. ...
  9. void GLWidget::initializeGL()
  10. {
  11. glClearColor(1.0,1.0,1.0,1.0);
  12. GLUquadricObj *qobj = gluNewQuadric();
  13. gluQuadricDrawStyle(qobj,GLU_FILL);
  14. gluQuadricNormals(qobj,GLU_SMOOTH);
  15. glClearDepth( 1.0f );
  16. glEnable(GL_DEPTH_TEST);
  17. glShadeModel(GL_SMOOTH);
  18. double eqn[] = {0.01f,0.0f,0.01f,-1.0f};// enable clip plane
  19. glClipPlane(GL_CLIP_PLANE0,eqn);
  20. setupLight();
  21. }
  22.  
  23.  
  24.  
  25. void GLWidget::paintGL()
  26. {
  27.  
  28. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  29. glMatrixMode(GL_MODELVIEW);
  30. glPushMatrix();
  31. glLoadIdentity();
  32. glTranslatef(0.0, 0.0, -5.0);
  33. glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
  34. glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
  35. glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
  36. //glTranslated(xTrans / 20.0, yTrans/ 20.0, 0.0f);
  37. glScalef(scale, scale, 1.0);
  38. m.drawControlFunction();
  39. glPopMatrix();
  40.  
  41. }
  42.  
  43. //ok for now
  44. void GLWidget::resizeGL(int w, int h){
  45.  
  46. int side = qMin(w, h);
  47. double ratio = (double)w/(double)h;
  48. //glViewport((w - side) / 2, (h - side) / 2, side, side);
  49. glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  50. glMatrixMode(GL_PROJECTION);
  51. glLoadIdentity();
  52. gluPerspective(70.0f, ratio, 0.1f, 1000.0f);
  53. // glOrtho(-15.0, +15.0, -10.0, +15.0, 1.0, 15.0);
  54.  
  55. glMatrixMode(GL_MODELVIEW);
  56. glLoadIdentity ();
  57. glTranslatef(0.0f, 0.0f, -10.0f);
  58. }
  59.  
  60.  
  61.  
  62. ...
  63.  
  64. void GLWidget::updateScreen(void)
  65. {
  66.  
  67. qDebug()<<"Im working";
  68.  
  69. updateGL();
  70. update();
  71. repaint();
  72. }
To copy to clipboard, switch view to plain text mode