Results 1 to 2 of 2

Thread: Drawing with QPainter in a QGLWidget

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Drawing with QPainter in a QGLWidget

    I have a QGLWidget that renders fine when using paintGL and also when using paintEvent() however I can't seem to get any painter.draw* commands to work with the paintEvent. I don't understand why this program won't allow me to draw with QPainter. I have mimicked the Overpainting example the best I could, however I am not clear what belongs in paintEvent (I think I know) and what belongs in initializeGL. The code I am posting shows both versions of the code I am trying to execute. All the code that is commented out is mostly the opengl calls used without overriding paintEvent, with the exception of the painter code at the bottom of the paintEvent which is the exact code from the overpainting exampe. I am simply trying to draw a line from the top corner of the widget to the bottom and still not able to get it to render properly.

    Qt Code:
    1. #include "glwidget.h"
    2. #include "../../shared/math3d.h"
    3. #include "../../shared/gltools.h"
    4.  
    5. #include <QPainter>
    6. #include <QPaintEvent>
    7.  
    8. #include <GL/glu.h>
    9.  
    10. #include <math.h>
    11.  
    12. GLWidget::GLWidget(QWidget *parent)
    13. : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
    14. {
    15. animationTimer.setSingleShot(false);
    16. animationTimer.setInterval(33);
    17. connect(&animationTimer, SIGNAL(timeout()), this, SLOT(update()));
    18. animationTimer.start();
    19.  
    20. qDebug() << pixmap.load("../Transformgl/graphicsview-pixmapitem.png");
    21. qDebug() << pixmap.width();
    22. qDebug() << pixmap.height();
    23.  
    24. setAutoFillBackground(false);
    25. setMinimumSize(800, 600);
    26. setWindowTitle("Transformgl with PaintEvent");
    27. }
    28.  
    29. QSize GLWidget::sizeHint() const
    30. {
    31. return QSize(800, 600);
    32. }
    33.  
    34. void GLWidget::initializeGL()
    35. {
    36. // Bluish background
    37. glClearColor(0.0f, 0.0f, .50f, 1.0f );
    38.  
    39. // Draw everything as wire frame
    40. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    41. }
    42.  
    43. void GLWidget::resizeGL(int w, int h)
    44. {
    45. // GLfloat fAspect;
    46.  
    47. // // Prevent a divide by zero, when window is too short
    48. // // (you cant make a window of zero width).
    49. // if(h == 0)
    50. // h = 1;
    51.  
    52. // glViewport(0, 0, w, h);
    53.  
    54. // fAspect = (GLfloat)w / (GLfloat)h;
    55.  
    56. // // Reset the coordinate system before modifying
    57. // glMatrixMode(GL_PROJECTION);
    58. // glLoadIdentity();
    59.  
    60. // // Set the clipping volume
    61. // gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
    62.  
    63. // glMatrixMode(GL_MODELVIEW);
    64. // glLoadIdentity();
    65.  
    66. setupViewport(w, h);
    67. }
    68.  
    69. //void GLWidget::paintGL()
    70. //{
    71. // M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;
    72.  
    73. // static GLfloat yRot = 0.0f; // Rotation angle for animation
    74. // yRot += 0.5f;
    75.  
    76. // // Clear the window with current clearing color
    77. // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    78.  
    79. // // Build a rotation matrix
    80. // m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
    81. // transformationMatrix[12] = 0.0f;
    82. // transformationMatrix[13] = 0.0f;
    83. // transformationMatrix[14] = -2.5f;
    84.  
    85. // glLoadMatrixf(transformationMatrix);
    86.  
    87. // gltDrawTorus(0.35f, 0.15f, 40, 20);
    88.  
    89. // // Do the buffer Swap
    90. // swapBuffers();
    91. //}
    92.  
    93. void GLWidget::paintEvent(QPaintEvent *event)
    94. {
    95. makeCurrent();
    96.  
    97. glMatrixMode(GL_MODELVIEW);
    98. glPushMatrix();
    99.  
    100. // Bluish background
    101. glClearColor(0.0f, 0.0f, .50f, 1.0f );
    102.  
    103. // Draw everything as wire frame
    104. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    105.  
    106. M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;
    107.  
    108. static GLfloat yRot = 0.0f; // Rotation angle for animation
    109. yRot += 0.5f;
    110.  
    111. setupViewport(width(), height());
    112.  
    113. // Clear the window with current clearing color
    114. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    115. glLoadIdentity();
    116.  
    117. // Build a rotation matrix
    118. m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
    119. transformationMatrix[12] = 0.0f;
    120. transformationMatrix[13] = 0.0f;
    121. transformationMatrix[14] = -2.5f;
    122.  
    123. glLoadMatrixf(transformationMatrix);
    124.  
    125. gltDrawTorus(0.35f, 0.15f, 40, 20);
    126.  
    127. glMatrixMode(GL_MODELVIEW);
    128. glPopMatrix();
    129.  
    130. QPainter painter(this);
    131. painter.setBrush(Qt::cyan);
    132. painter.drawLine(0, 0, width(), height());
    133. // QString text = tr("Click and drag with the left mouse button "
    134. // "to rotate the Qt logo.");
    135. // QFontMetrics metrics = QFontMetrics(font());
    136. // int border = qMax(4, metrics.leading());
    137.  
    138. // QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
    139. // Qt::AlignCenter | Qt::TextWordWrap, text);
    140. // painter.setRenderHint(QPainter::TextAntialiasing);
    141. // painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
    142. // QColor(0, 0, 0, 127));
    143. // painter.setPen(Qt::white);
    144. // painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
    145. // QColor(0, 0, 0, 127));
    146. // painter.drawText((width() - rect.width())/2, border,
    147. // rect.width(), rect.height(),
    148. // Qt::AlignCenter | Qt::TextWordWrap, text);
    149. painter.end();
    150. }
    151.  
    152. void GLWidget::setupViewport(int w, int h)
    153. {
    154. GLfloat fAspect;
    155.  
    156. // Prevent a divide by zero, when window is too short
    157. // (you cant make a window of zero width).
    158. if(h == 0)
    159. h = 1;
    160.  
    161. glViewport(0, 0, w, h);
    162.  
    163. fAspect = (GLfloat)w / (GLfloat)h;
    164.  
    165. // Reset the coordinate system before modifying
    166. glMatrixMode(GL_PROJECTION);
    167. glLoadIdentity();
    168.  
    169. // Set the clipping volume
    170. gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
    171.  
    172. glMatrixMode(GL_MODELVIEW);
    173. glLoadIdentity();
    174. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Angry Drawing with QPainter in a QGLWidget

    I have a QGLWidget that renders fine when using paintGL and also when using paintEvent() however I can't seem to get any painter.draw* commands to work with the paintEvent. I don't understand why this program won't allow me to draw with QPainter. I have mimicked the Overpainting example the best I could, however I am not clear what belongs in paintEvent (I think I know) and what belongs in initializeGL. The code I am posting shows both versions of the code I am trying to execute. All the code that is commented out is mostly the opengl calls used without overriding paintEvent, with the exception of the painter code at the bottom of the paintEvent which is the exact code from the overpainting exampe. I am simply trying to draw a line from the top corner of the widget to the bottom and still not able to get it to render properly.

    Qt Code:
    1. #include "glwidget.h"
    2. #include "../../shared/math3d.h"
    3. #include "../../shared/gltools.h"
    4.  
    5. #include <QPainter>
    6. #include <QPaintEvent>
    7.  
    8. #include <GL/glu.h>
    9.  
    10. #include <math.h>
    11.  
    12. GLWidget::GLWidget(QWidget *parent)
    13. : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
    14. {
    15. animationTimer.setSingleShot(false);
    16. animationTimer.setInterval(33);
    17. connect(&animationTimer, SIGNAL(timeout()), this, SLOT(update()));
    18. animationTimer.start();
    19.  
    20. qDebug() << pixmap.load("../Transformgl/graphicsview-pixmapitem.png");
    21. qDebug() << pixmap.width();
    22. qDebug() << pixmap.height();
    23.  
    24. setAutoFillBackground(false);
    25. setMinimumSize(800, 600);
    26. setWindowTitle("Transformgl with PaintEvent");
    27. }
    28.  
    29. QSize GLWidget::sizeHint() const
    30. {
    31. return QSize(800, 600);
    32. }
    33.  
    34. void GLWidget::initializeGL()
    35. {
    36. // Bluish background
    37. glClearColor(0.0f, 0.0f, .50f, 1.0f );
    38.  
    39. // Draw everything as wire frame
    40. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    41. }
    42.  
    43. void GLWidget::resizeGL(int w, int h)
    44. {
    45. // GLfloat fAspect;
    46.  
    47. // // Prevent a divide by zero, when window is too short
    48. // // (you cant make a window of zero width).
    49. // if(h == 0)
    50. // h = 1;
    51.  
    52. // glViewport(0, 0, w, h);
    53.  
    54. // fAspect = (GLfloat)w / (GLfloat)h;
    55.  
    56. // // Reset the coordinate system before modifying
    57. // glMatrixMode(GL_PROJECTION);
    58. // glLoadIdentity();
    59.  
    60. // // Set the clipping volume
    61. // gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
    62.  
    63. // glMatrixMode(GL_MODELVIEW);
    64. // glLoadIdentity();
    65.  
    66. setupViewport(w, h);
    67. }
    68.  
    69. //void GLWidget::paintGL()
    70. //{
    71. // M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;
    72.  
    73. // static GLfloat yRot = 0.0f; // Rotation angle for animation
    74. // yRot += 0.5f;
    75.  
    76. // // Clear the window with current clearing color
    77. // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    78.  
    79. // // Build a rotation matrix
    80. // m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
    81. // transformationMatrix[12] = 0.0f;
    82. // transformationMatrix[13] = 0.0f;
    83. // transformationMatrix[14] = -2.5f;
    84.  
    85. // glLoadMatrixf(transformationMatrix);
    86.  
    87. // gltDrawTorus(0.35f, 0.15f, 40, 20);
    88.  
    89. // // Do the buffer Swap
    90. // swapBuffers();
    91. //}
    92.  
    93. void GLWidget::paintEvent(QPaintEvent *event)
    94. {
    95. makeCurrent();
    96.  
    97. glMatrixMode(GL_MODELVIEW);
    98. glPushMatrix();
    99.  
    100. // Bluish background
    101. glClearColor(0.0f, 0.0f, .50f, 1.0f );
    102.  
    103. // Draw everything as wire frame
    104. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    105.  
    106. M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;
    107.  
    108. static GLfloat yRot = 0.0f; // Rotation angle for animation
    109. yRot += 0.5f;
    110.  
    111. setupViewport(width(), height());
    112.  
    113. // Clear the window with current clearing color
    114. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    115. glLoadIdentity();
    116.  
    117. // Build a rotation matrix
    118. m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
    119. transformationMatrix[12] = 0.0f;
    120. transformationMatrix[13] = 0.0f;
    121. transformationMatrix[14] = -2.5f;
    122.  
    123. glLoadMatrixf(transformationMatrix);
    124.  
    125. gltDrawTorus(0.35f, 0.15f, 40, 20);
    126.  
    127. glMatrixMode(GL_MODELVIEW);
    128. glPopMatrix();
    129.  
    130. QPainter painter(this);
    131. painter.setBrush(Qt::cyan);
    132. painter.drawLine(0, 0, width(), height());
    133. // QString text = tr("Click and drag with the left mouse button "
    134. // "to rotate the Qt logo.");
    135. // QFontMetrics metrics = QFontMetrics(font());
    136. // int border = qMax(4, metrics.leading());
    137.  
    138. // QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
    139. // Qt::AlignCenter | Qt::TextWordWrap, text);
    140. // painter.setRenderHint(QPainter::TextAntialiasing);
    141. // painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
    142. // QColor(0, 0, 0, 127));
    143. // painter.setPen(Qt::white);
    144. // painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
    145. // QColor(0, 0, 0, 127));
    146. // painter.drawText((width() - rect.width())/2, border,
    147. // rect.width(), rect.height(),
    148. // Qt::AlignCenter | Qt::TextWordWrap, text);
    149. painter.end();
    150. }
    151.  
    152. void GLWidget::setupViewport(int w, int h)
    153. {
    154. GLfloat fAspect;
    155.  
    156. // Prevent a divide by zero, when window is too short
    157. // (you cant make a window of zero width).
    158. if(h == 0)
    159. h = 1;
    160.  
    161. glViewport(0, 0, w, h);
    162.  
    163. fAspect = (GLfloat)w / (GLfloat)h;
    164.  
    165. // Reset the coordinate system before modifying
    166. glMatrixMode(GL_PROJECTION);
    167. glLoadIdentity();
    168.  
    169. // Set the clipping volume
    170. gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
    171.  
    172. glMatrixMode(GL_MODELVIEW);
    173. glLoadIdentity();
    174. }
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated as I am not sure what else to try to get the painter to draw correctly. Thanks!

Similar Threads

  1. QGLWidget Not Drawing Outside Of paintGL()
    By steadi in forum Newbie
    Replies: 7
    Last Post: 8th December 2012, 15:39
  2. Drawing primitives outside QGLWIdget
    By beginQT in forum Newbie
    Replies: 2
    Last Post: 27th February 2012, 11:03
  3. OpenGL drawing outside QGLWidget
    By zzz9 in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2012, 15:34
  4. Replies: 0
    Last Post: 28th April 2011, 18:40
  5. qglwidget not always drawing
    By spraff in forum Qt Programming
    Replies: 3
    Last Post: 24th June 2009, 19:44

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.