Question

I need to create a widget in a separate thread and to set MainWindow for it as a parent widget. Creation of a thread cannot be avoided.

In the constructor of a new widget I am specifying a pointer to MainWindow, but give

QObject::setParent: Cannot set parent, new parent is in a different thread

How to solve this?

P.S. Child widgets may be numerous.

Était-ce utile?

La solution 2

This is not possible. See the following code reference for details why not:

QObject source code

In particular, you would need to pay attention to this warning:

"qWarning("QObject::setParent: Cannot set parent, new parent is in a different thread");"

which you got on the command line based on your question, so this is all expected.

As the warning says, you need to make sure that the parenting happens in the same thread between the parent and child.

Creation of a thread cannot be avoided. How to solve this?

I am afraid you will need to refactor the code by either moveing this out of your thread into the same where the parent is or/and not have the separate thread at all.

Based on the information in your question, currently, it is not possible to say more since we do not yet completely know the functionality of your other thread.

Hope this helps with explaining it.

Autres conseils

You cannot create UI widgets outside of main thread

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top