PDA

View Full Version : Updating QGLWidget in Qt from Main Window



mcodesmart
15th May 2014, 17:20
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**




class MainWindow : public QMainWindow
{
Q_OBJECT
...
signals:
void draw(void);
...

**Slot**


class GLWidget : public QGLWidget
{
Q_OBJECT
..
public slots:
void updateScreen(void);
...

and in MainWindow.cpp
I do





void MainWindow::on_drawButton_clicked()
{
//do stuff to draw function in paintGL
emit draw();
qDebug()<<"draw emmited";
..



I connect them in MainWindows Constructor as follows..


GLWidget *g = new GLWidget;
connect(this, SIGNAL(draw()), g, SLOT(updateScreen()));


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


void GLWidget::updateScreen(void)
{
qDebug()<<"I'm Updating!";
updateGL();
update();
}

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


//temporary fix
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateScreen()));
timer->start(1000);

My GLWidget.cpp file is below..





...

#include "glwidget.h"
#include "motion.h"
#include "MainWindow.h"
#include <GL/glut.h>

...
void GLWidget::initializeGL()
{
glClearColor(1.0,1.0,1.0,1.0);
GLUquadricObj *qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluQuadricNormals(qobj,GLU_SMOOTH);
glClearDepth( 1.0f );
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
double eqn[] = {0.01f,0.0f,0.01f,-1.0f};// enable clip plane
glClipPlane(GL_CLIP_PLANE0,eqn);
setupLight();
}



void GLWidget::paintGL()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
//glTranslated(xTrans / 20.0, yTrans/ 20.0, 0.0f);
glScalef(scale, scale, 1.0);
m.drawControlFunction();
glPopMatrix();

}

//ok for now
void GLWidget::resizeGL(int w, int h){

int side = qMin(w, h);
double ratio = (double)w/(double)h;
//glViewport((w - side) / 2, (h - side) / 2, side, side);
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, ratio, 0.1f, 1000.0f);
// glOrtho(-15.0, +15.0, -10.0, +15.0, 1.0, 15.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glTranslatef(0.0f, 0.0f, -10.0f);
}



...

void GLWidget::updateScreen(void)
{

qDebug()<<"Im working";

updateGL();
update();
repaint();
}