Results 1 to 4 of 4

Thread: QGLWidget override not updating

  1. #1
    Join Date
    Apr 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QGLWidget override not updating

    So, I'm trying bring over some code to a Qt project I'm working on. Previously I had a interaction class which allowed me to interact with an openGL window. I'm trying to use the same code (modified for Qt) to allow me to interact with a QGLWidget.

    The problem being, that there is no change to the initial output. The code plots the initial input, but after that, their is no change due to mouse events, keyboard events, etc. There is a lot more code, but hopefully this should be sufficient to answer my question. I'm also a bit of a novice and just started playing with Qt, so with any luck it's not something too stupid.

    QGLWidget Header:

    Qt Code:
    1. #ifndef GLWIDGET_H
    2. #define GLWIDGET_H
    3.  
    4. #include <QGLWidget>
    5. #include "Point2D.h"
    6.  
    7. class GLWidget : public QGLWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. GLWidget(QWidget *parent);
    13. ~GLWidget();
    14.  
    15. void initializeGL();
    16. void resizeGL(int, int);
    17. void paintGL();
    18. void mousePressEvent(QMouseEvent *);
    19. void mouseReleaseEvent(QMouseEvent *);
    20. void mouseMoveEvent(QMouseEvent *);
    21. void keyPressEvent(unsigned char, int, int);
    22.  
    23. private:
    24. static double m_xRotate;
    25. static double m_yRotate;
    26. static double m_zRotate;
    27. static double m_xTrans;
    28. static double m_yTrans;
    29. static double m_zTrans;
    30. static Point2D m_LDownPos;
    31. static Point2D m_RDownPos;
    32. static Point2D m_MDownPos;
    33. static int m_LButtonDown;
    34. static int m_RButtonDown;
    35. static int m_MButtonDown;
    36.  
    37. };
    38.  
    39. #endif // GLWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    QGLWidget Class:

    Qt Code:
    1. #include "glwidget.h"
    2. #include <QMouseEvent>
    3. #include <stdlib.h>
    4. #include <GL\glut.h>
    5.  
    6. #include "Curve.h"
    7. #include "Color.h"
    8.  
    9.  
    10. #define TRUE 1
    11. #define FALSE 0
    12.  
    13. extern unsigned C;
    14.  
    15. Point2D GLWidget::m_LDownPos;
    16. Point2D GLWidget::m_RDownPos;
    17. Point2D GLWidget::m_MDownPos;
    18. int GLWidget::m_LButtonDown;
    19. int GLWidget::m_RButtonDown;
    20. int GLWidget::m_MButtonDown;
    21. double GLWidget::m_xRotate;
    22. double GLWidget::m_yRotate;
    23. double GLWidget::m_zRotate;
    24. double GLWidget::m_xTrans;
    25. double GLWidget::m_yTrans;
    26. double GLWidget::m_zTrans;
    27.  
    28.  
    29. GLWidget::GLWidget(QWidget *parent)
    30. : QGLWidget(parent)
    31. {
    32.  
    33. }
    34.  
    35. GLWidget::~GLWidget()
    36. {
    37.  
    38. }
    39.  
    40. void GLWidget::initializeGL()
    41. {
    42.  
    43. glClearColor(1.0, 1.0, 1.0, 1.0);
    44.  
    45. GLUquadricObj *qobj = gluNewQuadric();
    46. gluQuadricDrawStyle(qobj, GLU_FILL);
    47. gluQuadricNormals(qobj, GLU_SMOOTH);
    48.  
    49.  
    50. //glClearColor (0.0, 0.0, 0.0, 1.0);
    51. glEnable(GL_DEPTH_TEST);
    52. glShadeModel(GL_SMOOTH);
    53. // specify the back of the buffer as clear depth
    54. glClearDepth(1.0f);
    55.  
    56. double eqn[] = { 0.01f,0.0f,0.01f,-1.0f };
    57. // enable clip plane
    58. glClipPlane(GL_CLIP_PLANE0, eqn);
    59.  
    60. float LightAmbient[] = { 1.0f, 1.0f, 1.0f, 1.0 };
    61. float LightDiffuse[] = { 0.7f, 0.7f, 0.7f, 1.0f };
    62. float LightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    63. float LightPosition[] = { 3.0f, 3.0f, 3.0f, 1.0f };
    64.  
    65. float RedSurface[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    66. float GreenSurface[] = { 0.0f, 1.0f, 0.0f, 1.0f };
    67. float BlueSurface[] = { 0.0f, 0.0f, 1.0f, 1.0f };
    68.  
    69. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    70. glShadeModel(GL_SMOOTH);
    71.  
    72. glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
    73. glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
    74. glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
    75. glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
    76. glEnable(GL_LIGHTING);
    77. glEnable(GL_LIGHT0);
    78.  
    79. glDepthFunc(GL_LESS);
    80. glEnable(GL_DEPTH_TEST);
    81. glEnable(GL_NORMALIZE);
    82.  
    83. float no_mat[] = { 0.0f,0.0f,0.0f,1.0f };
    84. float mat_ambient[] = { 0.7f,0.7f,0.7f,1.0f };
    85. float mat_ambient_color[] = { 0.8f,0.3f,0.7f,1.0f };
    86. float mat_diffuse[] = { 0.7f,0.7f,0.7f,1.0f };
    87.  
    88. float mat_specular[] = { 0.7f,0.7f,0.7f,1.0f };
    89.  
    90. float no_shininess[] = { 0.0 };
    91. float low_shininess[] = { 5.0 };
    92. float high_shininess[] = { 100.0 };
    93. float mat_emission[] = { 0.2f,0.1f,0.1f,0.0f };
    94.  
    95. glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
    96. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    97. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    98. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    99. glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
    100. //glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
    101.  
    102. }
    103.  
    104. void GLWidget::resizeGL(int w, int h)
    105. {
    106. //Setup Viewing Transform: This calculates windows coordinates
    107. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    108.  
    109. //Setup Projection: Device coordinates
    110. glMatrixMode(GL_PROJECTION);
    111. glLoadIdentity();
    112. gluPerspective(30.0f, (double)w / (double)h, 0.1f, 1000.0f);
    113.  
    114. //Set up Modeling and Viewing transform: Get Eye coordinates
    115. glMatrixMode(GL_MODELVIEW);
    116. glLoadIdentity();
    117. glTranslatef(0.0f, 0.0f, -10.0f);
    118.  
    119. }
    120.  
    121. void GLWidget::paintGL()
    122. {
    123. static Curve m;
    124. m.calculateSwitch();
    125.  
    126. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    127.  
    128. glPushMatrix();
    129. glTranslated(m_xTrans, m_yTrans, m_zTrans);
    130. glRotated(m_xRotate, 1.0, 0.0, 0.0);
    131. glRotated(m_yRotate, 0.0, 1.0, 0.0);
    132. glRotated(m_zRotate, 0.0, 0.0, 1.0);
    133.  
    134. glMaterialfv(GL_FRONT, GL_DIFFUSE, BROWN);
    135.  
    136. m.drawCtrlPositions();
    137.  
    138. if (C == 1)
    139. {
    140. m.readCtrlPositions("input.txt", "v");
    141. m.drawCtrlPositions();
    142. }
    143.  
    144. glPopMatrix();
    145.  
    146. swapBuffers();
    147. }
    148.  
    149. void GLWidget::mousePressEvent(QMouseEvent *e)
    150. {
    151. QMouseEvent *m = e;
    152. int x = m->x();
    153. int y = m->y();
    154.  
    155. switch (m->button())
    156. {
    157. case Qt::LeftButton:
    158. m_LButtonDown = TRUE;
    159. m_LDownPos.setPx(x);
    160. m_LDownPos.setPy(y);
    161. break;
    162. case Qt::RightButton:
    163. m_RButtonDown = TRUE;
    164. m_RDownPos.setPx(x);
    165. m_RDownPos.setPy(y);
    166. break;
    167. case Qt::MiddleButton:
    168. m_MButtonDown = TRUE;
    169. m_MDownPos.setPx(x);
    170. m_MDownPos.setPy(y);
    171. break;
    172. default:
    173. break;
    174.  
    175. } //End of switch
    176.  
    177. updateGL();
    178. }
    179.  
    180. void GLWidget::mouseReleaseEvent(QMouseEvent *e)
    181. {
    182. QMouseEvent *m = e;
    183.  
    184. switch (m->button())
    185. {
    186. case Qt::LeftButton:
    187. m_LButtonDown = FALSE;
    188. break;
    189. case Qt::RightButton:
    190. m_RButtonDown = FALSE;
    191. break;
    192. case Qt::MiddleButton:
    193. m_MButtonDown = FALSE;
    194. break;
    195. default:
    196. break;
    197.  
    198. } //End of switch
    199.  
    200. updateGL();
    201. }
    202.  
    203. void GLWidget::mouseMoveEvent(QMouseEvent *e)
    204. {
    205. QMouseEvent *m = e;
    206. int x = m->x();
    207. int y = m->y();
    208.  
    209. /* find out what action to perform */
    210. if (m->button() == Qt::LeftButton)
    211. {
    212. m_yRotate -= (m_LDownPos.getPx() - (double)x) / 2.0;
    213. m_xRotate -= (m_LDownPos.getPy() - (double)y) / 2.0;
    214.  
    215. m_LDownPos.setPx(x);
    216. m_LDownPos.setPy(y);
    217. }
    218.  
    219. if (m->button() == Qt::RightButton)
    220. {
    221. m_xTrans -= (m_RDownPos.getPx() - (double)x) / 10.0f;
    222. m_yTrans += (m_RDownPos.getPy() - (double)y) / 10.0f;
    223.  
    224. m_RDownPos.setPx(x);
    225. m_RDownPos.setPy(y);
    226. }
    227.  
    228. if (m->button() == Qt::MiddleButton)
    229. {
    230. m_zTrans -= (m_MDownPos.getPy() - (double)y) / 10.0f;
    231.  
    232. m_MDownPos.setPx(x);
    233. m_MDownPos.setPy(y);
    234. }
    235.  
    236. updateGL();
    237. }
    238.  
    239. void GLWidget::keyPressEvent(unsigned char Key, int x, int y)
    240. {
    241. switch (Key)
    242. {
    243. case 27:
    244. case 'q':
    245. exit(0);
    246. break;
    247. };
    248.  
    249. updateGL();
    250. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGLWidget override not updating

    First, what is the point of making all of your coordinate and transform variables static members of the class? They aren't used in a way that would require it, and in fact will prevent you from using another instance of the same class in a different window - all the variables will have the same value everywhere.

    Not only that, but if I am following the code correctly, they never get initialized before they are used in paintGL(). m_xTrans, for example doesn't appear to have any value assigned until the mouseMoveEvent occurs, and then only if the right button is pressed. So until you move the mouse with the right button pressed, it has whatever value the compiler decided to randomize it to. Likewise all of your other variables.

    I think your problem is more a lack of C++ experience than it is a lack of Qt knowledge.

    First, what is the point of making all of your coordinate and transform variables static members of the class? They aren't used in a way that would require it, and in fact will prevent you from using another instance of the same class in a different window - all the variables will have the same value everywhere.

    Not only that, but if I am following the code correctly, they never get initialized before they are used in paintGL(). m_xTrans, for example doesn't appear to have any value assigned until the mouseMoveEvent occurs, and then only if the right button is pressed. So until you move the mouse with the right button pressed, it has whatever value the compiler decided to randomize it to. Likewise all of your other variables.

    I think your problem is more a lack of C++ experience than it is a lack of Qt knowledge.

  3. #3
    Join Date
    Apr 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGLWidget override not updating

    Hey, thanks for the response. So, I followed your recommendation and removed the static condition from the member variables. I also initialize the variables to zero. This gave me the same output as before.

    This still leaves me with the same problem I was having before. An inability to interact with the QGLWidget. I suspect the mouse events are being calculated, but not displayed for some reason as there is an increase in processor usage when I attempt to interact with the widget. Would you be able to help me solve this issue?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGLWidget override not updating

    Don't know why my comment double-posted. The site hiccuped when I clicked "Post Quick Reply" and I guess that's the result.

    You have some mystery code in there - like the static Curve variable "m" that calls "calculateSwitch() and then is used to "drawCtrlPositions()".

    By the way, you have a call to swapBuffers() at the end of paintGL(). You know that QGLWidget automatically swaps buffers by default on exit from paintGL(), right? (QGLWidget::setAutoBufferSwap()) So maybe your failure to see any changes is because as soon as you swap buffers to the updated one, QGLWidget swaps back to the old one.

Similar Threads

  1. Updating QGLWidget in Qt from Main Window
    By mcodesmart in forum Qt Programming
    Replies: 0
    Last Post: 15th May 2014, 17:20
  2. Replies: 1
    Last Post: 29th September 2012, 09:42
  3. Can't override the closeEvent()
    By scot_hansen in forum Newbie
    Replies: 7
    Last Post: 5th October 2010, 23:47
  4. How to override ShowEvent() etc
    By marcvanriet in forum Newbie
    Replies: 1
    Last Post: 22nd September 2010, 11:13
  5. Override a signal
    By daviddoria in forum Newbie
    Replies: 4
    Last Post: 9th February 2010, 19:18

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.