OpenGL painter->fillRect Question
-
I've been working at learning to program with Qt and OpenGL. So far, I've been playing with overlays and painting images on the screen. This evening however, I was attempting to set up a small program where the User would click a button and some text would be written onto the screen.
I started by creating a simple Widget class based on the QGLWidget base class. When the widget load's it displays a simple green background color. When a Users clicks a button it should write some text on the screen. The code to write to the screen comes in from "buttonClick()" which is wired to a SIGNAL/SLOT combination.
When the text is written on the screen, the color disappears. I suspect that I'm either not refreshing something correctly or I'm not drawing something is the correct order. Either way, I'm not sure what is not happening. I've posted the code for the class below. Any help would be appreciated!
@
#include "glwidget.h"GLWidget::GLWidget(QGLWidget *parent) :
QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
//Set the initial colors
qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
}GLWidget::~GLWidget()
{
//Do nothing
}QSize GLWidget::minimumSizeHint() const
{
return QSize(50, 50);
}QSize GLWidget::sizeHint() const
{
return QSize(400, 400);
}void GLWidget::initializeGL()
{
qglClearColor(qtGreen.dark());glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_MULTISAMPLE); static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 }; glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
}
void GLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);glMatrixMode(GL_PROJECTION); glLoadIdentity();
#ifdef QT_OPENGL_ES_1
glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
#else
glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
#endif
glMatrixMode(GL_MODELVIEW);
}void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
}void GLWidget::buttonClick()
{
QPainter painter(this);drawInstructions(&painter); painter.end();
}
void GLWidget::drawInstructions(QPainter *painter)
{QString text = tr("Some instructions."); 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(1, 0, 0, 255)); painter->setPen(Qt::white); painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border), QColor(1, 0, 0, 255)); painter->drawText((width() - rect.width())/2, border, rect.width(), rect.height(), Qt::AlignCenter | Qt::TextWordWrap, text);
}
@
-
You are trying to mix OpenGL rendering with QPainter painting and you are also trying to paint on the widget outside of the paint event.
If you want to ue QPainter in addition to OpenGL - ie not using OpenGL to draw all of yoru scene then I suggest you have a read of the the "OpenGL overpainting example":http://doc.qt.nokia.com/latest/opengl-overpainting.html.