Solved Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?
-
@Crawl.W Well, without source code others can only guess...
-
@Crawl.W said in Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?:
noting.
You are doing something or you would not have this error message.
If you don't want to show your code, then you will have to find yourself where you create a QObject instance with a parent in a different thread. -
I had assumed that the question only need simple description.Code has supplymented.
-
now it's obvious.
Your singleton is created in the main thread. you write to it inside the worker thread. during write process, a QTimer is created and set, to monitor timeouts. That is created from the wrong thread of course. -> Therefore the error
-
@J.Hilk So it is,You rocks!But I did not find where
QTimer
created, can you post those source? -
-
-
@J.Hilk As show in source,the
QTimer
is very strange, which is single shot and no interval set.Why?Why do not call his timeout's slot function directly? -
@Crawl.W said in Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?:
Why do not call his timeout's slot function directly?
Because then it would be a synchronous call - write() would block until it's finished. But since it is an asynchronous API it should not block the caller.
-
@Crawl.W said in Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?:
Why do not call his timeout's slot function directly?
This is done to call the timeout function after all events in thread queue have been processed.
Here the extract for QTimer::interval:The default value for this property is 0. A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed
-
@jsulm Great,I got it!Thanks.