Cannot create children for a parent that is in a different thread
-
I have a thread that is "listening" for some parameters and a widget that want to "consume" and elaborate those to show some resoult on the screen.This scenario is called after button click.
The code i'm trying to execute is this:
void Example::on_pushButton_clicked() { dialog *dia = new dialog(); GetterThread *thread = new GetterThread; thread->start(); dialog->exec(); connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection ); }
During the execution of the code i get this error:
QObject: Cannot create children for a parent that is in a different thread.The problem is that Dialog is running on the GUI Thread and Elaborate is on the GetterThread.
So I have tryied to add this line of code but is not working because dialog is a Widget...
QObject: Cannot create children for a parent that is in a different thread.dialog->moveToThread(thread);
Can someone gie me some hints to understand what i'm doing wrong?
-
From your code
connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection );
}And you said
Elaborate is on the GetterThread ??? are you true ?
connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection ); // dia is the slot's class ?
Qt manual
QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)
Note:
- sender and sender's signal;
- receiver and receiver's slot;
-
From your code
connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection );
}And you said
Elaborate is on the GetterThread ??? are you true ?
connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection ); // dia is the slot's class ?
Qt manual
QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)
Note:
- sender and sender's signal;
- receiver and receiver's slot;
@joeQ said in Cannot create children for a parent that is in a different thread:
And you said
Elaborate is on the GetterThread ??? are you true ?
Well is obviously a misstype, i inverted the GUI Thread And the GetterThread.
There is really no need to be rude.
I'm here to learn and understand what i'm doing wrong.Yes dia is the SLOT class.
-
I have a thread that is "listening" for some parameters and a widget that want to "consume" and elaborate those to show some resoult on the screen.This scenario is called after button click.
The code i'm trying to execute is this:
void Example::on_pushButton_clicked() { dialog *dia = new dialog(); GetterThread *thread = new GetterThread; thread->start(); dialog->exec(); connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection ); }
During the execution of the code i get this error:
QObject: Cannot create children for a parent that is in a different thread.The problem is that Dialog is running on the GUI Thread and Elaborate is on the GetterThread.
So I have tryied to add this line of code but is not working because dialog is a Widget...
QObject: Cannot create children for a parent that is in a different thread.dialog->moveToThread(thread);
Can someone gie me some hints to understand what i'm doing wrong?
@Bruschetta What is not clear to me: is this "elaborate" a GUI (widget, dialog,...) class?
Never do anything related to GUI classes in another threads! All widgets have to be in the main thread. -
@joeQ said in Cannot create children for a parent that is in a different thread:
And you said
Elaborate is on the GetterThread ??? are you true ?
Well is obviously a misstype, i inverted the GUI Thread And the GetterThread.
There is really no need to be rude.
I'm here to learn and understand what i'm doing wrong.Yes dia is the SLOT class.
@Bruschetta Oh, Hi friend. Sorry what i said. and welcome. Please forgive me . I come from China, so i am not good with English. I was not rude. I am sorry.
About your question:
My solutions:
- You can try this way.
void Example::on_pushButton_clicked() { dialog *dia = new dialog(); GetterThread *thread = new GetterThread; connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection ); thread->start(); dialog->exec(); }
- Second way.
GetterThread::GetterThread(dialog *pDialog)//GetterThread Constructor { connect( this, SIGNAL(sendTwoWords( const QString &,const QString &)), pDialog, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection ); } void Example::on_pushButton_clicked() { dialog *dia = new dialog(); GetterThread *thread = new GetterThread(dia); thread->start(); dialog->exec(); }
I am very sorry for what I said just now. best wish for you.
-
@Bruschetta What is not clear to me: is this "elaborate" a GUI (widget, dialog,...) class?
Never do anything related to GUI classes in another threads! All widgets have to be in the main thread.@jsulm
Dia is a Widget, a widget that have to show something(the strings it's getting from the signal for example).
Dia is created in the GUI Thread, sendTwoWords is the SIGNAL from the GetterThread;
So the widgets is in the main thread(GUI) but i'm unable to get data from the GetterThread to show in the widgetThe comunication problem is infact between GUI Thread e GetterThread
QObject: Cannot create children for a parent that is in a different thread. (Parent is GetterThread(0x2e1fbb50), parent's thread is QThread(0x202daa70), current thread is GetterThread(0x2e1fbb50)
@joeQ you are wlecome, is not easy to comunicate even with our motherlanguage ;)
I have tryied the examples you gave me but the problem is the same :( Still getting the above quoted message.
Anyway thanx for your advices, time and hints -
@jsulm
Dia is a Widget, a widget that have to show something(the strings it's getting from the signal for example).
Dia is created in the GUI Thread, sendTwoWords is the SIGNAL from the GetterThread;
So the widgets is in the main thread(GUI) but i'm unable to get data from the GetterThread to show in the widgetThe comunication problem is infact between GUI Thread e GetterThread
QObject: Cannot create children for a parent that is in a different thread. (Parent is GetterThread(0x2e1fbb50), parent's thread is QThread(0x202daa70), current thread is GetterThread(0x2e1fbb50)
@joeQ you are wlecome, is not easy to comunicate even with our motherlanguage ;)
I have tryied the examples you gave me but the problem is the same :( Still getting the above quoted message.
Anyway thanx for your advices, time and hints@Bruschetta The question is why you get "QObject: Cannot create children for a parent that is in a different thread." It is not related to the connect statement. You should check what you are doing in that thread. Do you allocate any objects with GetterThread as parent?
-
@Bruschetta The question is why you get "QObject: Cannot create children for a parent that is in a different thread." It is not related to the connect statement. You should check what you are doing in that thread. Do you allocate any objects with GetterThread as parent?
@jsulm You where right....
socket=new QTcpSocket(this); //not Working
socket=new QTcpSocket();//WorkingThanks a lot for the answer. You made my day ;)
-
@jsulm You where right....
socket=new QTcpSocket(this); //not Working
socket=new QTcpSocket();//WorkingThanks a lot for the answer. You made my day ;)
@Bruschetta Is this code located in the constructor? If so then move it to run(). Constructor is executed in the thread creating the GetterThread instance, so everything you create in constructor will be in that thread. But then you use socket in the new thread when start() is called.
-
@Bruschetta Is this code located in the constructor? If so then move it to run(). Constructor is executed in the thread creating the GetterThread instance, so everything you create in constructor will be in that thread. But then you use socket in the new thread when start() is called.
@jsulm
Thank you for the tip but the code is already in the run() -
@jsulm
Thank you for the tip but the code is already in the run()@Bruschetta You're right: thread object (GetterThread) itself lives in the main thread, but not the socket :-) So the thread object cannot be parent of socket.