Unsolved Qt event emit inside thread not working properly
-
void loaddepth::run(){ QString file_name=this->filename; int day=this->day_no; int depth=this->deepth_no; QFile *file=new QFile(file_name); QTextStream in(file); file->open(QIODevice::ReadOnly | QIODevice::Text); const auto size = file->size(); QStringList strList; bool flag=false; QStringList list; int line=0; int day_count=0; while(!in.atEnd()&&day_count<=day) { QString str=in.readLine().simplified(); QStringList list=str.split(' '); if(list.count()==3){ if(flag){ **emit readStatusChanged(this->file_no,list[0],list[1],list[2].toDouble());** } } else if(list.count()==1){ if(list[0].toInt()!=depth&&flag==true){ qDebug()<<"b4 senting"; } if(list[0].toInt()==depth){ flag=true; } } line++; } } ****************************** connect(fileData1,SIGNAL(readStatusChanged(int,QString,QString,double)),this,SLOT(onReadStatusChanged(int,QString,QString,double))); **event emits is not triggering inside the thread loop.**
-
Did you check if the connect is successful and if you really emit the signal? Please show us where you connect the signal/slot connection.
-
@Christian-Ehrlicher ya emit works only outside while loop. it doesn't work inside the loop.
connect(fileData1,SIGNAL(readStatusChanged(int,QString,QString,double)),this,SLOT(onReadStatusChanged(int,QString,QString,double)));
-
@sujith-D said in Qt event emit inside thread not working properly:
ya emit works only outside while loop. it doesn't work inside the loop.
Again: how did you check that the signal is emitted? Did you added a breakpoint? Or a debug output?
And you still did not check the return value of connect() and did not show us the code around the connect on how you instantiate the class etc.
-
I do qDebug(), and its output.
-
@sujith-D said in Qt event emit inside thread not working properly:
I do qDebug(), and its output.
Again: please show us more code, best a minimal example so we can try to reproduce it.
-
@Christian-Ehrlicher
Calling thread from module2. Declared signals and slots in corresponding header files.
I don't know why the same event not working inside the loop during the file read.Module2::Module2(QWidget *parent) : QWidget(parent), rect(0), ui(new Ui::Module2), scaleFactor(1), mapDim(1500) { fileData1=new loaddepth(this); fileData1->filename=QCoreApplication::applicationDirPath()+"/../project3DUnderWater/0.txt"; fileData1->deepth_no=1; connect(fileData1,SIGNAL(readStatusChanged(int,QString,QString,double)),this,SLOT(onReadStatusChanged(int,QString,QString,double))); connect(fileData1,SIGNAL(ReadFileCompleted(int,QJsonObject)),this,SLOT(onReadFileCompleted(int,QJsonObject))); fileData1->start(); } void Module2::onReadStatusChanged(int file_no,QString y,QString x,double z ){ qDebug()<<"file_no"<<file_no; } void Module2::onReadFileCompleted(int file_no,QJsonObject json){ qDebug() <<json; }
module2 header file
#include<QProgressDialog> namespace Ui { class Module2; } class Module2 : public QWidget { Q_OBJECT public: explicit Module2(QWidget *parent = nullptr); ~Module2(); loaddepth *fileData1; private slots: void onReadStatusChanged(int,QString,QString,double); void onReadFileCompleted(int,QJsonObject); };
Thread header file
#include<QtCore> class loaddepth:public QThread { Q_OBJECT public: loaddepth(QObject *parent=0); void run() override; bool stop; QString filename; int day_no; int deepth_no; QJsonObject obj; int file_no; signals: void readStatusChanged(int,QString,QString,double); void ReadFileCompleted(int,QJsonObject); };
-
This is working fine for me:
#include <QtCore> class MyThread : public QThread { Q_OBJECT public: using QThread::QThread; void run() override { while (true) { emit emitSignal(); QThread::sleep(2); } } Q_SIGNALS: void emitSignal(); }; class MyObject : public QObject { Q_OBJECT public: MyObject(QObject *parent = nullptr) : QObject(parent) { MyThread *mt = new MyThread(this); connect(mt, &MyThread::emitSignal, this, &MyObject::mySlot); mt->start(); } private Q_SLOTS: void mySlot() { qDebug() << "Got signal!"; } }; int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); MyObject obj; return app.exec(); } #include "main.moc"
Btw: no need to create (and leak) a QFile object on the heap.