PDA

View Full Version : Drawing with QPainter in a QGLWidget



jshafferman
2nd May 2014, 20:08
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.



#include "glwidget.h"
#include "../../shared/math3d.h"
#include "../../shared/gltools.h"

#include <QPainter>
#include <QPaintEvent>

#include <GL/glu.h>

#include <math.h>

GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
animationTimer.setSingleShot(false);
animationTimer.setInterval(33);
connect(&animationTimer, SIGNAL(timeout()), this, SLOT(update()));
animationTimer.start();

qDebug() << pixmap.load("../Transformgl/graphicsview-pixmapitem.png");
qDebug() << pixmap.width();
qDebug() << pixmap.height();

setAutoFillBackground(false);
setMinimumSize(800, 600);
setWindowTitle("Transformgl with PaintEvent");
}

QSize GLWidget::sizeHint() const
{
return QSize(800, 600);
}

void GLWidget::initializeGL()
{
// Bluish background
glClearColor(0.0f, 0.0f, .50f, 1.0f );

// Draw everything as wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}

void GLWidget::resizeGL(int w, int h)
{
// GLfloat fAspect;

// // Prevent a divide by zero, when window is too short
// // (you cant make a window of zero width).
// if(h == 0)
// h = 1;

// glViewport(0, 0, w, h);

// fAspect = (GLfloat)w / (GLfloat)h;

// // Reset the coordinate system before modifying
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();

// // Set the clipping volume
// gluPerspective(35.0f, fAspect, 1.0f, 50.0f);

// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();

setupViewport(w, h);
}

//void GLWidget::paintGL()
//{
// M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;

// static GLfloat yRot = 0.0f; // Rotation angle for animation
// yRot += 0.5f;

// // Clear the window with current clearing color
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// // Build a rotation matrix
// m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
// transformationMatrix[12] = 0.0f;
// transformationMatrix[13] = 0.0f;
// transformationMatrix[14] = -2.5f;

// glLoadMatrixf(transformationMatrix);

// gltDrawTorus(0.35f, 0.15f, 40, 20);

// // Do the buffer Swap
// swapBuffers();
//}

void GLWidget::paintEvent(QPaintEvent *event)
{
makeCurrent();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// Bluish background
glClearColor(0.0f, 0.0f, .50f, 1.0f );

// Draw everything as wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;

static GLfloat yRot = 0.0f; // Rotation angle for animation
yRot += 0.5f;

setupViewport(width(), height());

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Build a rotation matrix
m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
transformationMatrix[12] = 0.0f;
transformationMatrix[13] = 0.0f;
transformationMatrix[14] = -2.5f;

glLoadMatrixf(transformationMatrix);

gltDrawTorus(0.35f, 0.15f, 40, 20);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

QPainter painter(this);
painter.setBrush(Qt::cyan);
painter.drawLine(0, 0, width(), height());
// QString text = tr("Click and drag with the left mouse button "
// "to rotate the Qt logo.");
// QFontMetrics metrics = QFontMetrics(font());
// int border = qMax(4, metrics.leading());

// QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
// Qt::AlignCenter | Qt::TextWordWrap, text);
// painter.setRenderHint(QPainter::TextAntialiasing);
// painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
// QColor(0, 0, 0, 127));
// painter.setPen(Qt::white);
// painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
// QColor(0, 0, 0, 127));
// painter.drawText((width() - rect.width())/2, border,
// rect.width(), rect.height(),
// Qt::AlignCenter | Qt::TextWordWrap, text);
painter.end();
}

void GLWidget::setupViewport(int w, int h)
{
GLfloat fAspect;

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

glViewport(0, 0, w, h);

fAspect = (GLfloat)w / (GLfloat)h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the clipping volume
gluPerspective(35.0f, fAspect, 1.0f, 50.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

jshafferman
2nd May 2014, 20:09
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.



#include "glwidget.h"
#include "../../shared/math3d.h"
#include "../../shared/gltools.h"

#include <QPainter>
#include <QPaintEvent>

#include <GL/glu.h>

#include <math.h>

GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
animationTimer.setSingleShot(false);
animationTimer.setInterval(33);
connect(&animationTimer, SIGNAL(timeout()), this, SLOT(update()));
animationTimer.start();

qDebug() << pixmap.load("../Transformgl/graphicsview-pixmapitem.png");
qDebug() << pixmap.width();
qDebug() << pixmap.height();

setAutoFillBackground(false);
setMinimumSize(800, 600);
setWindowTitle("Transformgl with PaintEvent");
}

QSize GLWidget::sizeHint() const
{
return QSize(800, 600);
}

void GLWidget::initializeGL()
{
// Bluish background
glClearColor(0.0f, 0.0f, .50f, 1.0f );

// Draw everything as wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}

void GLWidget::resizeGL(int w, int h)
{
// GLfloat fAspect;

// // Prevent a divide by zero, when window is too short
// // (you cant make a window of zero width).
// if(h == 0)
// h = 1;

// glViewport(0, 0, w, h);

// fAspect = (GLfloat)w / (GLfloat)h;

// // Reset the coordinate system before modifying
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();

// // Set the clipping volume
// gluPerspective(35.0f, fAspect, 1.0f, 50.0f);

// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();

setupViewport(w, h);
}

//void GLWidget::paintGL()
//{
// M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;

// static GLfloat yRot = 0.0f; // Rotation angle for animation
// yRot += 0.5f;

// // Clear the window with current clearing color
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// // Build a rotation matrix
// m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
// transformationMatrix[12] = 0.0f;
// transformationMatrix[13] = 0.0f;
// transformationMatrix[14] = -2.5f;

// glLoadMatrixf(transformationMatrix);

// gltDrawTorus(0.35f, 0.15f, 40, 20);

// // Do the buffer Swap
// swapBuffers();
//}

void GLWidget::paintEvent(QPaintEvent *event)
{
makeCurrent();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// Bluish background
glClearColor(0.0f, 0.0f, .50f, 1.0f );

// Draw everything as wire frame
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

M3DMatrix44f rotationMatrix, translationMatrix, transformationMatrix;

static GLfloat yRot = 0.0f; // Rotation angle for animation
yRot += 0.5f;

setupViewport(width(), height());

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Build a rotation matrix
m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
transformationMatrix[12] = 0.0f;
transformationMatrix[13] = 0.0f;
transformationMatrix[14] = -2.5f;

glLoadMatrixf(transformationMatrix);

gltDrawTorus(0.35f, 0.15f, 40, 20);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

QPainter painter(this);
painter.setBrush(Qt::cyan);
painter.drawLine(0, 0, width(), height());
// QString text = tr("Click and drag with the left mouse button "
// "to rotate the Qt logo.");
// QFontMetrics metrics = QFontMetrics(font());
// int border = qMax(4, metrics.leading());

// QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
// Qt::AlignCenter | Qt::TextWordWrap, text);
// painter.setRenderHint(QPainter::TextAntialiasing);
// painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
// QColor(0, 0, 0, 127));
// painter.setPen(Qt::white);
// painter.fillRect(QRect(0, 0, width(), rect.height() + 2*border),
// QColor(0, 0, 0, 127));
// painter.drawText((width() - rect.width())/2, border,
// rect.width(), rect.height(),
// Qt::AlignCenter | Qt::TextWordWrap, text);
painter.end();
}

void GLWidget::setupViewport(int w, int h)
{
GLfloat fAspect;

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

glViewport(0, 0, w, h);

fAspect = (GLfloat)w / (GLfloat)h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the clipping volume
gluPerspective(35.0f, fAspect, 1.0f, 50.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


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