PDA

View Full Version : QT SIGNALS and SLOTS



beginQT
23rd September 2011, 13:12
Hello,

I am new to QT environment, I am using QT version 4.6.4 and also i am using QT integrated with Eclipse for my development environment.
I am facing a problem with button signals and slots. Please see below.

I have a Class "Example" which is extended from QMainWindow class, And, Part of the UI I have used for opengl window, which I named it as "GLWidget" and it extends from QGLWidget.
Now class "GLWidget" is children of "Example" as it is using the part of the main window. And, I have created the button in Mainwindow UI using Qpushbutton. and added a code in the Example Class constructor for calling a particular function in the GLWidget class by using this code below.

connect(ui.button,SIGNAL(clicked()),ui.glWindow,SL OT(vDrawPoly()));

But, it doesnot work, It would not give me the expected results. Could anyone tell me what's the problem?

Thanks in Advance!

Zlatomir
23rd September 2011, 13:23
What errors do you get? - Tell us more about what is happening (and post the class declaration if possible)

Also you can look at debug output (i don't know how you can do that in eclipse :o ) the framework "signals" all the connections problems.

beginQT
23rd September 2011, 13:40
I am not getting any Errors but the function that i am using as a SLOT doesn't get hit when I click on the button.
Here is my code:


#include "Example.h"
#include "GLWidget.h"


Example::Example(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.button,SIGNAL(clicked()),ui.Gl_Helper,S LOT(vDrawPoly()));

}

Example::~Example()
{

}

And the GLWidget code:

/*
* GLWidget.cpp
*
* Created on: Sep 22, 2011
*
*/
#include <QtOpenGL>
#include "GLWidget.h"



GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{

pos = 0.0f;

}

GLWidget::~GLWidget()
{
makeCurrent();
glDeleteLists(1, 1);
}

void GLWidget::initializeGL()
{
glClearColor(1.0f,0.0f,0.0f,0.0f);
// object = makeObject();

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 1.0f, 1.0f, 1.0, 1.0f };
GLfloat specularLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat position[] = { -100.0f, 100.0f, 0.0f, 1.0f };

// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);


glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}

void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(0.0f,0.0f, 0.0f);
//glScaled(scale,scale,scale);
glRotated(0.0f, 1.0f, 0.0f, 0.0f);
glRotated(0.0f, 0.0f, 1.0f, 0.0f);
glRotated(0.0f, 0.0f, 0.0f, 1.0f);
glCallList(1);


glPushMatrix();
glTranslated(sPs.fX,sPs.fY, sPs.fZ);
glRotatef(sR.fAngle,sR.fX_R,sR.fY_R,sR.fZ_R);
glColor3f(sClr.fR, sClr.fG, sClr.fB);
glLineWidth(5.0f);
glBegin(GL_POLYGON);
glVertex2d(-pos, pos);
glVertex2d(-pos, -pos);
glVertex2d(pos, -pos);
glVertex2d(pos, pos);
glEnd();
glPopMatrix();

//vDrawPoly();



}

void GLWidget::resizeGL(int width, int height)
{

glViewport(0.0f, 0.0f, 800, 600);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
glOrtho(-200, 300, -300, 300, -300, 300.0);
glMatrixMode(GL_MODELVIEW);
}

void GLWidget::mousePressEvent(QMouseEvent *event)
{
// lastPos = event->pos();
}

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{

}

void GLWidget::vDrawPoly()
{
sPs.vInit(0.0f,0.0f,0.0f);
sR.vInit(0.0f,0.0f,0.0f,0.0f);
sClr.vInit(1.0f, 1.0f, 1.0f);
pos = 50;
updateGL();
/*glPushMatrix();
glTranslated(10.0f,10.0f, 1.0f);
glRotatef(0.0f,0.0f,0.0f,0.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glLineWidth(5.0f);
glBegin(GL_POLYGON);
glVertex2d(-50.0f, 50.0f);
glVertex2d(-50.0f, -50.0f);
glVertex2d(50.0f, -50.0f);
glVertex2d(50.0f, 50.0f);
glEnd();
glPopMatrix();*/

}

The function that i have written for SLOT is vDrawPoly(), doesn't get called. I have posted only code in .cpp files as .h isnot requred and i also have .ui file which is generated.

paie
23rd September 2011, 13:56
Please resubmit your code example with the code tags around your code.

In "Advanced" mode, highlight your code and press the # edit tool.

Please resubmit your code example with the code tags around your code.

In "Advanced" mode, highlight your code and press the # edit tool.

Here is your post with the proper formatting.
:)


I am not getting any Errors but the function that i am using as a SLOT doesn't get hit when I click on the button.
Here is my code:



#include "Example.h"
#include "GLWidget.h"


Example::Example(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.button,SIGNAL(clicked()),ui.Gl_Helper,S LOT(vDrawPoly()));

}

Example::~Example()
{

}

And the GLWidget code:
/*
* GLWidget.cpp
*
* Created on: Sep 22, 2011
*
*/
#include <QtOpenGL>
#include "GLWidget.h"



GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{

pos = 0.0f;

}

GLWidget::~GLWidget()
{
makeCurrent();
glDeleteLists(1, 1);
}

void GLWidget::initializeGL()
{
glClearColor(1.0f,0.0f,0.0f,0.0f);
// object = makeObject();

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 1.0f, 1.0f, 1.0, 1.0f };
GLfloat specularLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat position[] = { -100.0f, 100.0f, 0.0f, 1.0f };

// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);


glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}

void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(0.0f,0.0f, 0.0f);
//glScaled(scale,scale,scale);
glRotated(0.0f, 1.0f, 0.0f, 0.0f);
glRotated(0.0f, 0.0f, 1.0f, 0.0f);
glRotated(0.0f, 0.0f, 0.0f, 1.0f);
glCallList(1);


glPushMatrix();
glTranslated(sPs.fX,sPs.fY, sPs.fZ);
glRotatef(sR.fAngle,sR.fX_R,sR.fY_R,sR.fZ_R);
glColor3f(sClr.fR, sClr.fG, sClr.fB);
glLineWidth(5.0f);
glBegin(GL_POLYGON);
glVertex2d(-pos, pos);
glVertex2d(-pos, -pos);
glVertex2d(pos, -pos);
glVertex2d(pos, pos);
glEnd();
glPopMatrix();

//vDrawPoly();



}

void GLWidget::resizeGL(int width, int height)
{

glViewport(0.0f, 0.0f, 800, 600);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
glOrtho(-200, 300, -300, 300, -300, 300.0);
glMatrixMode(GL_MODELVIEW);
}

void GLWidget::mousePressEvent(QMouseEvent *event)
{
// lastPos = event->pos();
}

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{

}

void GLWidget::vDrawPoly()
{
sPs.vInit(0.0f,0.0f,0.0f);
sR.vInit(0.0f,0.0f,0.0f,0.0f);
sClr.vInit(1.0f, 1.0f, 1.0f);
pos = 50;
updateGL();
/*glPushMatrix();
glTranslated(10.0f,10.0f, 1.0f);
glRotatef(0.0f,0.0f,0.0f,0.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glLineWidth(5.0f);
glBegin(GL_POLYGON);
glVertex2d(-50.0f, 50.0f);
glVertex2d(-50.0f, -50.0f);
glVertex2d(50.0f, -50.0f);
glVertex2d(50.0f, 50.0f);
glEnd();
glPopMatrix();*/

}


The function that i have written for SLOT is vDrawPoly(), doesn't get called. I have posted only code in .cpp files as .h isnot requred and i also have .ui file which is generated.

Zlatomir
23rd September 2011, 14:00
Post the "Example.h" file - so that we can see your class "layout". //Use code tags - they make the code more "readable"

Make sure that button is the name you gave to the QPushButton widget into Designer and void vDrawPoly(); is declared with public slots: access specifier.

Also see how you can see the Debug output - because the framework prints the signal-slot connections "errors" (and then you can see what have you done wrong).

beginQT
23rd September 2011, 14:31
Here is my Example.h file.


#include <QtGui/QMainWindow>
#include "ui_psx_hmi.h"

class Example : public QMainWindow
{
Q_OBJECT

public:
Example(QWidget *parent = 0);
~Example();



private:
Ui::ExampleClass ui;
GLWidget *gl;

protected:
void resizeEvent ( QResizeEvent * event );
};

#endif

Zlatomir
23rd September 2011, 14:37
Try:

connect(ui.button,SIGNAL(clicked()), gl,SLOT(vDrawPoly())); //gl is the address of the QOBJECT you want to connect (and it is not from the ui object)


Also make sure the gl pointer gets initialized before the connect is executed (object must be created for the connect to work) and that the "slot_function" is declared with public slots: access specifier (in GLWidget.h file)

beginQT
23rd September 2011, 14:40
You mentioned that vDrawPoly() should be in Public slots: section, is it in Examples class or GLWidget Class?

Thanks!

Great work! Thanks a lot! that works!

I did not placed the DrawPoly() in the public slots..