I have found an old thread that came close to answering this very question for me, its link can be found here QObject: Cannot create children for a parent that is in a different thread .
I know this is an old thread, but I am having a hard time following where to put the signals and slots in my situation. I am writing a program that will send out alerts via email, and I want them to be run in threads so they don't interrupt the main program and each other. I created an EmailThread class that inherits from QThread and implements the run() method. I have another class, Smtp.cpp, that successfully sends emails out but only in the main thread. I would like to create an instance of the Smtp class in each EmailThread for each address that should receive an email. I also have a namespace that handles all my database information. I want to be able to send information from my database to all the entered email addresses if something goes wrong. So which class gets the slots and which one emits the signals? And where would I place them, code examples to help me figure it out would be greatly appreciated.
//EmailThread Header
#ifndef EMAILTHREAD_H
#define EMAILTHREAD_H
#include <QThread>
#include "smtp.h"
class EmailThread : public QThread
{
public:
explicit EmailThread(QString to, QString from, QString subject, QString message);
signals:
public slots:
protected:
void run();
private:
Smtp * emailer;
QString to;
QString from;
QString subject;
QString message;
};
#endif // EMAILTHREAD_H
//EmailThread.cpp
#include "emailthread.h"
EmailThread::EmailThread(QString emailTo, QString emailFrom, QString emailSubject, QString emailMessage)
{
emailer = new Smtp("myaddress@email.com","myPassword", "secure.emailsrvr.com",465, 300000);
to = emailTo;
from = emailFrom;
subject = emailSubject;
message = emailMessage;
}
void EmailThread::run(){
emailer->sendMailNoAttachments(from,to, subject, message);
}
Called Email
EmailThread * newEmailThread = new EmailThread("myaddress@email.com","myaddress@email.com", "Subject Test", "This is the message string");
newEmailThread->start();
and I get the same error as above, where the parent and child thread are not the same. Here is the full error :
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSslSocket(0xc6dde0), parent's thread is QThread(0xf52410), current thread is QThread(0xc619c8)
QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketState'
(Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType().)
Thank you for your consideration!