1

How can call update() on QQuickItem from a worker thread without causing the following warning?

Updates can only be scheduled from GUI thread or from QQuickItem::updatePaintNode()

I want to enforce QQuickItem::updatePaintNode() to execute.

I tried the solution of hooking a QObject::connect from the worker thread to a slot which calls MyQQuickItem->update(). Everything works fine though. QQuickItem::updatePaintNode() is called after the signal emit & the QQuickItem is updated the way I want. But I get this warning on every update call I schedule which I understand as you cannot update UI from worker thread. But what is the way to do this without a warning in Qt?

BUT, How can I get rid the warning that I get on every update call?

Note: I had to make the QObject::connect with a Qt::DirectConnection since a Qt::QueuedConnection did not work for calling update through a signal.

Checked through this discussion here. Discussion in this link ends with a complain about the same warning that I am getting here. My question is what should I correct to avoid that warning?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • read this: http://www.qtcentre.org/threads/64596-Using-QQuickView-in-separate-thread-problems – eyllanesc Aug 18 '17 at 07:52
  • @eyllanesc I've read that [link](http://www.qtcentre.org/threads/64596-Using-QQuickView-in-separate-thread-problems) you provided. As I mentioned in my question as well - I understand that one cannot call `update` from a worker thread. **But I thought Qt signal slot will take care of that as mentioned in that link ?** I not calling update directly as well. It is through a signal slot – TheWaterProgrammer Aug 18 '17 at 08:07
  • You must provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – eyllanesc Aug 18 '17 at 08:18
  • 1
    If you use `Qt::DirectConnection`, the slot is called in the worker thread, you should use `Qt::QueuedConnection` (or `Qt::AutoConnection`). Why isn't it working? – m7913d Aug 18 '17 at 19:12
  • @m7913d I have a `GLuint` in the signal & slot. if I use `Qt::AutoConnection`, it complains "cannot queue arguments of type GLuint. Make sure 'GLuint' is registered using qRegisterMetaType()`. `Qt::QueuedConnection` does not work at all. it doesnt even throw any error – TheWaterProgrammer Aug 18 '17 at 21:38
  • 1
    You should indeed register it to be able to use it in a queued connection. – m7913d Aug 18 '17 at 22:22
  • yes. queued connection works nicely across threads. thanks @m7913d – TheWaterProgrammer Aug 19 '17 at 16:51
  • also, I should not pass by reference in a signal slot across threads? (*UI thread & worker thread in this case*) Is my understanding correct?. I want to pass const references but reading through the signal slot internals, I see that it is un-advisable to pass by reference? Please confirm – TheWaterProgrammer Aug 19 '17 at 16:53
  • 1
    According to [this link](http://www.embeddeduse.com/2013/06/29/copied-or-not-copied-arguments-signals-slots/), you should pass it always by reference. Qt will take care of creating some copies when necessary (to implement the asynchronous behavior/queued connection) – m7913d Aug 19 '17 at 19:58
  • Note that Qt itself declares its arguments as const references (for complex datatypes) – m7913d Aug 19 '17 at 20:01
  • ok. looks like I should pass by const ref & let Qt create extra copies if required. thanks for sharing the info @m7913d – TheWaterProgrammer Aug 20 '17 at 16:36

0 Answers0