PDA

View Full Version : problem on QGLWidget and QGLFramebufferObject



xylosper
13th December 2008, 08:56
I wrote a simple code using QGLWidget and QGLFramebufferObject like this:

#include <QApplication>
#include <QTextDocument>
#include <QPainter>
#include <QGLFramebufferObject>
#include <QGLWidget>

class GLWidget : public QGLWidget {
public:
GLWidget(QWidget *parent = 0): QGLWidget(parent), fbo(0) {}
~GLWidget() {if (fbo) delete fbo;}
protected:
void initializeGL() {
fbo = new QGLFramebufferObject(200, 200);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
QTextDocument doc;
doc.setHtml("<font color=red>Test text</font>");
QPainter p(fbo);
doc.drawContents(&p, QRect(QPoint(0, 0), fbo->size()));
p.end();
}
void resizeGL(int w, int h) {
if(h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, (GLfloat) w, (GLfloat) h, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void paintEvent(QPaintEvent *event) {
QPainter painter(this);
QRect r = rect();
painter.fillRect(r, Qt::white);
r.moveTo(100, 0); // line1
painter.fillRect(r, Qt::black); // line2
drawTexture(QPoint(10, 10), fbo->texture());
}
private:
QGLFramebufferObject *fbo;
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
GLWidget w;
w.resize(200, 100);
w.show();
return app.exec();
}

I expected red texts, but the result is black texts like the attached file "tow-color.png"
http://www.qtcentre.org/forum/attachment.php?attachmentid=2753&stc=1&d=1229158073

I found out that the commenting out the line1 and line2 of the paintEvent() in the class changes the result like the attached file "one-color.png".
http://www.qtcentre.org/forum/attachment.php?attachmentid=2752&stc=1&d=1229158073

What's going on?
And, how can I get red texts with the original code?

kwisp
15th December 2008, 07:56
I think, when you fillRect(...,Qt::black), you setBrush to black color.

May be -


painter.fillRect(r, Qt::black); // line2
painter.setBrush(Qt::red);// ???
drawTexture(QPoint(10, 10), fbo->texture());

:)

xylosper
15th December 2008, 11:57
Thanks for answering.

The color of brush doesn't matter, because the texts are already rendered by QTextDocument and all informations are in fbo's texture.
In addition to that, I tried your code actually, but nothing is changed.