[Interest] Odd crash with QPainter + QOpenGLWidget

John Weeks john at wavemetrics.com
Wed Mar 27 23:18:52 CET 2019


I can't really answer your exact question, but I can tell you that we render QPainter drawing (including text) onto a QOpenGLWidget this way (not the real code...):

QOpenGLWidget::paintGL()
{
	QPainter painter(this);

	painter->beginNativePainting();
	.... do your OpenGL drawing here ...
	painter->endNativePainting();

	... do your QPainter drawing here ...

	return true;
}

But in fact, the part  that says, "... do your QPainter drawing here ..." is actually done by creating an appropriately-sized QImage, doing the QPainter drawing in that, then drawing the QImage into the original QPainter. There is a comment in the code:

 		// JW 161012 This next block is a work-around for what appears to be a Qt bug in drawing
		// text into a QOpenGLWidget that contains a texture (?). Text drawn here comes out sheared
		// in a strange way (you may be able to still see this in the drag image when dragging an
		// annotation). Details of the bug depend on the font and which characters are being drawn.

My vague recollection is that we did it this way because using paintEvent() requires some slight of hand to sync the GL buffers. Maybe the crash is a result of something like that.

-John Weeks

> On Mar 27, 2019, at 2:48 PM, Matthew Woehlke <mwoehlke.floss at gmail.com> wrote:
> 
> I've cargo-culted some code to render text over a QOpenGLWidget from
> another project:
> 
>  void MyWidget::paintEvent(QPaintEvent* event)
>  {
>    QOpenGLWidget::paintEvent(event);
> 
>    auto const& text = /* elided */;
> 
>    QPainter painter{this};
>    painter.setPen(Qt::white);
>    painter.setFont(font());
>    painter.drawText(rect(), Qt::AlignCenter, text);
>    painter.end();
>  }
> 
> In the "donor" project, this works fine, but the "recipient" project is
> crashing on exit:
> 
> ==12997== Invalid read of size 8
> ==12997==    at 0x5D204E4: QObject::thread() const (qobject.cpp:1420)
> ==12997==    by 0x598A728: QOpenGLVertexArrayObjectPrivate::destroy()
> (qopenglvertexarrayobject.cpp:212)
> ==12997==    by 0x598A9AA:
> QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject()
> (qopenglvertexarrayobject.cpp:392)
> ==12997==    by 0x5987286:
> QOpenGLTextureGlyphCache::~QOpenGLTextureGlyphCache()
> (qopengltextureglyphcache.cpp:87)
> ==12997==    by 0x59872AC:
> QOpenGLTextureGlyphCache::~QOpenGLTextureGlyphCache()
> (qopengltextureglyphcache.cpp:93)
> ==12997==    by 0x576ADAB: ~QLinkedListNode (qlinkedlist.h:69)
> ==12997==    by 0x576ADAB:
> QLinkedList<QFontEngine::GlyphCacheEntry>::freeData(QLinkedListData*)
> [clone .isra.119] (qlinkedlist.h:345)
> ==12997==    by 0x5BB10E8: QHashData::free_helper(void
> (*)(QHashData::Node*)) (qhash.cpp:572)
> ==12997==    by 0x576A9BA: freeData (qhash.h:585)
> ==12997==    by 0x576A9BA: ~QHash (qhash.h:254)
> ==12997==    by 0x576A9BA: QFontEngine::~QFontEngine() (qfontengine.cpp:271)
> ==12997==    by 0x18C155FC: QFontEngineFT::~QFontEngineFT()
> (qfontengine_ft.cpp:794)
> ==12997==    by 0x576AAE4: QFontEngineMulti::~QFontEngineMulti()
> (qfontengine.cpp:1792)
> ==12997==    by 0x18C1E41C:
> QFontEngineMultiFontConfig::~QFontEngineMultiFontConfig()
> (qfontenginemultifontconfig.cpp:57)
> ==12997==    by 0x575F25E: QFontCache::clear() (qfont.cpp:2796)
> ==12997==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
> 
> Any ideas why? Is this a reasonable approach to rendering text in a
> QOpenGLWidget, or is there a better way? (I am "fairly confident" that
> the problem is the text rendering, above, since the crash goes away if I
> comment out said code.)
> 
> -- 
> Matthew
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest




More information about the Interest mailing list