0

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.

1
  • Yes, I think so. I will try to create widgets in a common thread. The problem is how to do it, because I must use a special framework and those widgets must be extensions for the main UI. But this is another problem :) Commented Dec 30, 2013 at 21:41

2 Answers 2

3

You cannot create UI widgets outside of main thread

Sign up to request clarification or add additional context in comments.

3 Comments

Strictly speaking, this is not correct. You could create it. The main criteria is to have it in the same thread. It is not obligatory whether that is the main.
Just tested to create QWidget in separate thread and got runtime error (Qt 5.1)
Why did you test that? You cannot create it in separate thread, but you can create it outside the "main" thread. The only criteria is to have both in the same. See my answer for details.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.