Results 1 to 8 of 8

Thread: QT SIGNALS and SLOTS

  1. #1
    Join Date
    Sep 2011
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Unhappy QT SIGNALS and SLOTS

    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!

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT SIGNALS and SLOTS

    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 ) the framework "signals" all the connections problems.

  3. #3
    Join Date
    Sep 2011
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT SIGNALS and SLOTS

    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:

    Qt Code:
    1. #include "Example.h"
    2. #include "GLWidget.h"
    3.  
    4.  
    5. Example::Example(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. ui.setupUi(this);
    9. connect(ui.button,SIGNAL(clicked()),ui.Gl_Helper,SLOT(vDrawPoly()));
    10.  
    11. }
    12.  
    13. Example::~Example()
    14. {
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    And the GLWidget code:
    Qt Code:
    1. /*
    2.  * GLWidget.cpp
    3.  *
    4.  * Created on: Sep 22, 2011
    5.  *
    6.  */
    7. #include <QtOpenGL>
    8. #include "GLWidget.h"
    9.  
    10.  
    11.  
    12. GLWidget::GLWidget(QWidget *parent)
    13. : QGLWidget(parent)
    14. {
    15.  
    16. pos = 0.0f;
    17.  
    18. }
    19.  
    20. GLWidget::~GLWidget()
    21. {
    22. makeCurrent();
    23. glDeleteLists(1, 1);
    24. }
    25.  
    26. void GLWidget::initializeGL()
    27. {
    28. glClearColor(1.0f,0.0f,0.0f,0.0f);
    29. // object = makeObject();
    30.  
    31. glEnable(GL_LIGHTING);
    32. glEnable(GL_LIGHT0);
    33.  
    34. // Create light components
    35. GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    36. GLfloat diffuseLight[] = { 1.0f, 1.0f, 1.0, 1.0f };
    37. GLfloat specularLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    38. GLfloat position[] = { -100.0f, 100.0f, 0.0f, 1.0f };
    39.  
    40. // Assign created components to GL_LIGHT0
    41. glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    42. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    43. glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    44. glLightfv(GL_LIGHT0, GL_POSITION, position);
    45.  
    46.  
    47. glShadeModel(GL_FLAT);
    48. glEnable(GL_DEPTH_TEST);
    49. glEnable(GL_CULL_FACE);
    50. }
    51.  
    52. void GLWidget::paintGL()
    53. {
    54. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    55. glLoadIdentity();
    56. glTranslated(0.0f,0.0f, 0.0f);
    57. //glScaled(scale,scale,scale);
    58. glRotated(0.0f, 1.0f, 0.0f, 0.0f);
    59. glRotated(0.0f, 0.0f, 1.0f, 0.0f);
    60. glRotated(0.0f, 0.0f, 0.0f, 1.0f);
    61. glCallList(1);
    62.  
    63.  
    64. glPushMatrix();
    65. glTranslated(sPs.fX,sPs.fY, sPs.fZ);
    66. glRotatef(sR.fAngle,sR.fX_R,sR.fY_R,sR.fZ_R);
    67. glColor3f(sClr.fR, sClr.fG, sClr.fB);
    68. glLineWidth(5.0f);
    69. glBegin(GL_POLYGON);
    70. glVertex2d(-pos, pos);
    71. glVertex2d(-pos, -pos);
    72. glVertex2d(pos, -pos);
    73. glVertex2d(pos, pos);
    74. glEnd();
    75. glPopMatrix();
    76.  
    77. //vDrawPoly();
    78.  
    79.  
    80.  
    81. }
    82.  
    83. void GLWidget::resizeGL(int width, int height)
    84. {
    85.  
    86. glViewport(0.0f, 0.0f, 800, 600);
    87.  
    88.  
    89. glMatrixMode(GL_PROJECTION);
    90. glLoadIdentity();
    91. //glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
    92. glOrtho(-200, 300, -300, 300, -300, 300.0);
    93. glMatrixMode(GL_MODELVIEW);
    94. }
    95.  
    96. void GLWidget::mousePressEvent(QMouseEvent *event)
    97. {
    98. // lastPos = event->pos();
    99. }
    100.  
    101. void GLWidget::mouseMoveEvent(QMouseEvent *event)
    102. {
    103.  
    104. }
    105.  
    106. void GLWidget::vDrawPoly()
    107. {
    108. sPs.vInit(0.0f,0.0f,0.0f);
    109. sR.vInit(0.0f,0.0f,0.0f,0.0f);
    110. sClr.vInit(1.0f, 1.0f, 1.0f);
    111. pos = 50;
    112. updateGL();
    113. /*glPushMatrix();
    114. glTranslated(10.0f,10.0f, 1.0f);
    115. glRotatef(0.0f,0.0f,0.0f,0.0f);
    116. glColor3f(1.0f, 1.0f, 1.0f);
    117. glLineWidth(5.0f);
    118. glBegin(GL_POLYGON);
    119. glVertex2d(-50.0f, 50.0f);
    120. glVertex2d(-50.0f, -50.0f);
    121. glVertex2d(50.0f, -50.0f);
    122. glVertex2d(50.0f, 50.0f);
    123. glEnd();
    124. glPopMatrix();*/
    125.  
    126. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by wysota; 23rd September 2011 at 23:58. Reason: missing [code] tags

  4. #4
    Join Date
    Aug 2011
    Posts
    33
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT SIGNALS and SLOTS

    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.


    Quote Originally Posted by beginQT View Post
    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:

    Qt Code:
    1. #include "Example.h"
    2. #include "GLWidget.h"
    3.  
    4.  
    5. Example::Example(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. ui.setupUi(this);
    9. connect(ui.button,SIGNAL(clicked()),ui.Gl_Helper,SLOT(vDrawPoly()));
    10.  
    11. }
    12.  
    13. Example::~Example()
    14. {
    15.  
    16. }
    17.  
    18. And the GLWidget code:
    19. /*
    20.  * GLWidget.cpp
    21.  *
    22.  * Created on: Sep 22, 2011
    23.  *
    24.  */
    25. #include <QtOpenGL>
    26. #include "GLWidget.h"
    27.  
    28.  
    29.  
    30. GLWidget::GLWidget(QWidget *parent)
    31. : QGLWidget(parent)
    32. {
    33.  
    34. pos = 0.0f;
    35.  
    36. }
    37.  
    38. GLWidget::~GLWidget()
    39. {
    40. makeCurrent();
    41. glDeleteLists(1, 1);
    42. }
    43.  
    44. void GLWidget::initializeGL()
    45. {
    46. glClearColor(1.0f,0.0f,0.0f,0.0f);
    47. // object = makeObject();
    48.  
    49. glEnable(GL_LIGHTING);
    50. glEnable(GL_LIGHT0);
    51.  
    52. // Create light components
    53. GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    54. GLfloat diffuseLight[] = { 1.0f, 1.0f, 1.0, 1.0f };
    55. GLfloat specularLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    56. GLfloat position[] = { -100.0f, 100.0f, 0.0f, 1.0f };
    57.  
    58. // Assign created components to GL_LIGHT0
    59. glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    60. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    61. glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    62. glLightfv(GL_LIGHT0, GL_POSITION, position);
    63.  
    64.  
    65. glShadeModel(GL_FLAT);
    66. glEnable(GL_DEPTH_TEST);
    67. glEnable(GL_CULL_FACE);
    68. }
    69.  
    70. void GLWidget::paintGL()
    71. {
    72. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    73. glLoadIdentity();
    74. glTranslated(0.0f,0.0f, 0.0f);
    75. //glScaled(scale,scale,scale);
    76. glRotated(0.0f, 1.0f, 0.0f, 0.0f);
    77. glRotated(0.0f, 0.0f, 1.0f, 0.0f);
    78. glRotated(0.0f, 0.0f, 0.0f, 1.0f);
    79. glCallList(1);
    80.  
    81.  
    82. glPushMatrix();
    83. glTranslated(sPs.fX,sPs.fY, sPs.fZ);
    84. glRotatef(sR.fAngle,sR.fX_R,sR.fY_R,sR.fZ_R);
    85. glColor3f(sClr.fR, sClr.fG, sClr.fB);
    86. glLineWidth(5.0f);
    87. glBegin(GL_POLYGON);
    88. glVertex2d(-pos, pos);
    89. glVertex2d(-pos, -pos);
    90. glVertex2d(pos, -pos);
    91. glVertex2d(pos, pos);
    92. glEnd();
    93. glPopMatrix();
    94.  
    95. //vDrawPoly();
    96.  
    97.  
    98.  
    99. }
    100.  
    101. void GLWidget::resizeGL(int width, int height)
    102. {
    103.  
    104. glViewport(0.0f, 0.0f, 800, 600);
    105.  
    106.  
    107. glMatrixMode(GL_PROJECTION);
    108. glLoadIdentity();
    109. //glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
    110. glOrtho(-200, 300, -300, 300, -300, 300.0);
    111. glMatrixMode(GL_MODELVIEW);
    112. }
    113.  
    114. void GLWidget::mousePressEvent(QMouseEvent *event)
    115. {
    116. // lastPos = event->pos();
    117. }
    118.  
    119. void GLWidget::mouseMoveEvent(QMouseEvent *event)
    120. {
    121.  
    122. }
    123.  
    124. void GLWidget::vDrawPoly()
    125. {
    126. sPs.vInit(0.0f,0.0f,0.0f);
    127. sR.vInit(0.0f,0.0f,0.0f,0.0f);
    128. sClr.vInit(1.0f, 1.0f, 1.0f);
    129. pos = 50;
    130. updateGL();
    131. /*glPushMatrix();
    132. glTranslated(10.0f,10.0f, 1.0f);
    133. glRotatef(0.0f,0.0f,0.0f,0.0f);
    134. glColor3f(1.0f, 1.0f, 1.0f);
    135. glLineWidth(5.0f);
    136. glBegin(GL_POLYGON);
    137. glVertex2d(-50.0f, 50.0f);
    138. glVertex2d(-50.0f, -50.0f);
    139. glVertex2d(50.0f, -50.0f);
    140. glVertex2d(50.0f, 50.0f);
    141. glEnd();
    142. glPopMatrix();*/
    143.  
    144. }
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT SIGNALS and SLOTS

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

  6. #6
    Join Date
    Sep 2011
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT SIGNALS and SLOTS

    Here is my Example.h file.

    Qt Code:
    1. #include <QtGui/QMainWindow>
    2. #include "ui_psx_hmi.h"
    3.  
    4. class Example : public QMainWindow
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. Example(QWidget *parent = 0);
    10. ~Example();
    11.  
    12.  
    13.  
    14. private:
    15. Ui::ExampleClass ui;
    16. GLWidget *gl;
    17.  
    18. protected:
    19. void resizeEvent ( QResizeEvent * event );
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 23rd September 2011 at 23:59. Reason: missing [code] tags

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT SIGNALS and SLOTS

    Try:
    Qt Code:
    1. 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)
    To copy to clipboard, switch view to plain text mode 

    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)

  8. #8
    Join Date
    Sep 2011
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT SIGNALS and SLOTS

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

Similar Threads

  1. Help with Signals and slots
    By chetu1984 in forum Newbie
    Replies: 5
    Last Post: 10th March 2011, 22:30
  2. Signals & Slots!
    By qtoptus in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2010, 01:50
  3. about signals and slots
    By Sandip in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2008, 16:02
  4. help with signals and slots
    By superutsav in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2006, 12:49
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 08:12

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.