PDA

View Full Version : Problem on drawing text of big font size on QGLWidget and QGLFramebufferObject



xylosper
11th March 2011, 03:33
I wrote some code for test:

#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>
#include <QtOpenGL/QGLFramebufferObject>

class GLWidget : public QGLWidget {
public:
GLWidget() {
m_big = font();
m_big.setPixelSize(100);
makeCurrent();
m_fbo = new QGLFramebufferObject(500, 150);
m_fbo->bind();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QPainter painter(m_fbo);
painter.setFont(m_big);
painter.drawText(QRect(0, 0, 500, 150), "Test Text");
m_fbo->release();
}
~GLWidget() {
delete m_fbo;
}
private:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.setFont(m_big);
painter.drawText(QRect(0, 0, 500, 150), "Test Text");
drawTexture(QPointF(0.0, 150.0), m_fbo->texture());
}
QFont m_big;
QGLFramebufferObject *m_fbo;
};

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


The above codes do drawing two lines of "Test Text" on QGLWidget.
Upper one is done by directly onto QGLWidget with QPainter, and the lower one is done by drawTexture whose texture is drawn in QGLFramebufferObject.

The result is attached.
6070

Both of them have problems.

Upper one(drawn by QGLWidget with QPainter) is not anti-aliased.
Lower one(drawn by QGLFramebufferObject) just shows black box.

Also, when I tried with multi-sampled QGLWidget(with like QGLFormat::setSamples(4)), Upper one looks as same as lower one, that is, only black box.

If you try with small font size, both of them display the texts normally.

Is this a bug? or Did I mistake something?

JohannesMunk
11th March 2011, 13:21
Hi!

1) Have you tried enabling High Quality OpenGL AntiAliasing on the painter?
painter.setRenderHints(QPainter::Antialiasing|QPai nter::TextAntialiasing|QPainter::SmoothPixmapTrans form|QPainter::HighQualityAntialiasing);

2) Is your big font exceeding the bounding rect you are passing to drawText?

3) You set your glClearColor to black. How can you see black text at all? You should probably put your gl initialization code into initializeGL for it to work.

HIH

Joh

xylosper
11th March 2011, 16:07
To JohannesMunk:

1) Nothing changes.

2) No. The bounding rect is large enough to display all texts.

3) It's for QGLFramebufferObject(fbo) not QGLWidget.
And, the clear color is not black, but transparent.
If you set it to (1.0, 1.0, 1.0, 1.0), the background color of fbo just becomes white, and still exists the problem.

Thank you for your reply.

JohannesMunk
11th March 2011, 16:16
1)

And, the clear color is not black, but transparent.

You are right! Missed that.

2) What happens if you take the same big font to a QGraphicsTextItem in a QGraphicsScene with an OpenGL Viewport?

3) As workaround: Why don't you take the smaller font and scale the painter before drawing?

Johannes

xylosper
13th March 2011, 11:53
Thanks again, Johannes.

I'll try later with QGraphicsScene.

Yes, I've already handled this problem with scaling.

imvic999
30th March 2018, 11:42
I got the same result with Qt 5.1.1 in using QGraphicsWebView to show web content.
Any idea or hint that I can solve this?

TKS~

Vic

imvic999
31st March 2018, 03:59
I found this issue was reported.
(https://bugreports.qt.io/browse/QTBUG-18094?jql=text%20~%20%22QGLWidget%20font%22)

Samuel mentioned,
GL text rendering falls back to path rendering when the font size grows too big. To get antialiasing the QGLWidget needs QGL::SampleBuffers in the QGLFormat, and the FBO needs the stencil buffer attachment for the path rendering to work.

I did add QGLFormat with QGL::StencilBuffer, however the text still a block.
QGLFormat glFormat(QGL::StencilBuffer | QGL::SampleBuffers);

Did I misunderstood what he means?

imvic999
31st March 2018, 10:36
I finally solve this issue by define QT_MAX_CACHED_GLYPH_SIZE = 128 (means maximum font size 127)
QT_MAX_CACHED_GLYPH_SIZE default set to 64.