<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Non GUI thread use]]></title><description><![CDATA[<p dir="auto">I make an unusual use of Qt: I've developped a Qt based library and I want to provide its services to Java too using a JNI (in addition to a standard C++ API).</p>
<p dir="auto">Here is how it currently works:<br />
when the library is initalized in the context of a stand-alone application, it creates a <em>QGraphicsScene</em> which is rendered in a <em>QGraphicsView</em> when the library is initalized in a Java context, it creates a <em>QGraphicsScene</em> which is rendered in a <em>QImage</em> at Java client request</p>
<p dir="auto">In both cases, I create a QApplication but in the Java context, I don't call QApplication::exec().<br />
The QGraphicsScene is modified by the time task of a QTimer.<br />
In the Java context, a QMutex ensures that the time task and the Java client request to get the scene bitmap data don't collide.</p>
<p dir="auto">It basically works but it also crashes randomly and here is my current investigation state.<br />
First of all, I've replaced the QPixmap with QImage since the final QImage rendering is made outside a GUI thread (and Qt complained about that).</p>
<p dir="auto">Now there is a single remaining warning at client startup:<br />
"QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread"</p>
<p dir="auto">Here is the code of the Java context rendering scheme:<br />
@<br />
QImage image (w, h, QImage::Format_ARGB32_Premultiplied);<br />
QPainter painter(&amp;image);<br />
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);<br />
fScene-&gt;render(&amp;painter);<br />
painter.end();<br />
@</p>
<p dir="auto">Note that it doesn't crash when I comment out the 'render' call.</p>
<p dir="auto">I don't get anymore warning from Qt and the developer documentation only talks about QWidget that should not be used in a non GUI thread. Most of the crashes occur when modifying a QGraphicsSceneBspTreeIndex (or QGraphicsSceneBspTreeIndexPrivate) object.<br />
It looks like there are remaining unsafe operations called. Any help would be greatly appreciated.</p>
<p dir="auto">[<em>edit: fixed paragraphs -mariusg</em>]</p>
]]></description><link>https://forum.qt.io/topic/2234/non-gui-thread-use</link><generator>RSS for Node</generator><lastBuildDate>Wed, 19 Aug 2026 20:21:57 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/2234.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 09 Dec 2010 07:59:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Non GUI thread use on Thu, 09 Dec 2010 17:43:16 GMT]]></title><description><![CDATA[<p dir="auto">Sorry, I've already posted a reply before but it seems to be lost...</p>
<p dir="auto">[quote author="Volker" date="1291895376"]If you do not start the main event loop with QApplication::exec() the posted events cannot be delivered between threads. That's the cause for the startup warning.[/quote]<br />
I don't think that's the reason of the crash. Anyway, I can't call exec() outside the main thread (got a warning).</p>
<p dir="auto">[quote author="Volker" date="1291895376"]Regarding the crashe, I don't have any clue, why this happens. Does the program work if you call it in Java mode without actually using Java (i.e. have a Qt test program, that calls your app in Java mode)?[/quote]</p>
<p dir="auto">That's a good idea, but the behavior changes:</p>
<h1>when I call QApplication::exec(), everything works fine</h1>
<h1>when I don't, it looks like my QTimer doesn't work, but if I call my time task manually, everything works fine</h1>
<p dir="auto">The latter could be logically explained since there is no application call to post the events (including the timer events). However, the question is "why does it work in Java?"</p>
<p dir="auto">Note that when I say "everything works fine", I don't make real graphic rendering, I only request the QImage data into a custom buffer (like the JNI do).<br />
I need to investigate more and to carefully check the various contexts.<br />
Thanks for your help.</p>
]]></description><link>https://forum.qt.io/post/64114</link><guid isPermaLink="true">https://forum.qt.io/post/64114</guid><dc:creator><![CDATA[fober]]></dc:creator><pubDate>Thu, 09 Dec 2010 17:43:16 GMT</pubDate></item><item><title><![CDATA[Reply to Non GUI thread use on Thu, 09 Dec 2010 11:49:36 GMT]]></title><description><![CDATA[<p dir="auto">If you do not start the main event loop with QApplication::exec() the posted events cannot be delivered between threads. That's the cause for the startup warning.</p>
<p dir="auto">Regarding the crashe, I don't have any clue, why this happens. Does the program work if you call it in Java mode without actually using Java (i.e. have a Qt test program, that calls your app in Java mode)?</p>
]]></description><link>https://forum.qt.io/post/64025</link><guid isPermaLink="true">https://forum.qt.io/post/64025</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Thu, 09 Dec 2010 11:49:36 GMT</pubDate></item></channel></rss>