Unsolved Slot connected to signal emitted from thread does not get called
-
I have defined a new slot in a class that inherits from QThread:
class MyThread : public QThread { Q_OBJECT public: MyThread(QString fileName); void run() override; signals: void addNewItem(int row, int col, QString text); private: QString fileName; };
In the Dialog class I have defined slot that should get called when the above signal will be emitted. This slot updates widget on UI:
void Dialog::onAddNewItem(int row, int col, QString text) { ui->tableWidget->setItem(row, col, new QTableWidgetItem(text)); }
In the constructor of the Dialog class I have created the working thread and connected signal to a slot:
worker = new MyThread(filePath); worker->run(); connect(worker, &MyThread::addNewItem, this, &Dialog::onAddNewItem);
In the run() function I'm emitting the addNewItem signal but the onAddNewItem slot never gets called. Why is that?
-
@michalt38 Hi, you might want to check the excellent checklist made by Giuseppe D'Angelo : https://www.kdab.com/slot-not-invoked/ . For instance, you could test the connect to return true or that the connect does not gets deleted following thread destruction...
-
Hi,
One thing: to start your thread, call the start method, run will be called automatically in the new thread. Here, you will have your run method executed and then you connect the signal. Hence the slot will never be called.