I want to display an error message whenever my independent thread encounters the word "alert1" in a specific .txt file. But I get the above error inside the monitorForAlerts() inside mythread.cpp file. The line expectedly executes if I were to place it inside dialog.cpp. So I guess this is due to non-inheritance of this object. Can you please advise me how to solve this error for the given code?

Here is the code:
dialog.h
Qt Code:
  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3.  
  4. #include <QDialog>
  5. #include <QtCore>
  6. #include "mythread.h"
  7. namespace Ui {
  8. class Dialog;
  9. }
  10.  
  11. class Dialog : public QDialog
  12. {
  13. Q_OBJECT
  14.  
  15. public:
  16. explicit Dialog(QWidget *parent = 0);
  17. ~Dialog();
  18.  
  19. public slots:
  20.  
  21. private:
  22. Ui::Dialog *ui;
  23.  
  24. private slots:
  25. void on_pushButton_clicked();
  26. void on_pushButton_2_clicked();
  27. };
  28.  
  29. #endif // DIALOG_H
To copy to clipboard, switch view to plain text mode 

mythread.h
Qt Code:
  1. #ifndef MYTHREAD_H
  2. #define MYTHREAD_H
  3.  
  4. #include <QThread>
  5. #include <QtCore>
  6. #include <QDebug>
  7. #include <QFile>
  8. #include <Windows.h>
  9. #include <QMessageBox>
  10. #include <QTimer>
  11. #define ALERTS_MESSAGE_STORAGE_PATH "E:\\QT1\\simpleGUIThread2\\simpleGUIThread2\\usbAlert.txt"
  12. #define TIMER_VALUE 500
  13. class MyThread : public QThread
  14. {
  15. Q_OBJECT
  16. public:
  17. explicit MyThread(QObject *parent = 0);
  18. void run();
  19. QString name;
  20. void monitorForAlerts();
  21. int exec();
  22.  
  23. public slots:
  24.  
  25. signals:
  26. void testSignal(QString message);
  27.  
  28. public slots:
  29.  
  30. };
  31.  
  32. #endif // MYTHREAD_H
To copy to clipboard, switch view to plain text mode 

dialog.cpp
Qt Code:
  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3.  
  4. Dialog::Dialog(QWidget *parent) :
  5. QDialog(parent),
  6. ui(new Ui::Dialog)
  7. {
  8. ui->setupUi(this);
  9. }
  10.  
  11. Dialog::~Dialog()
  12. {
  13. delete ui;
  14. }
  15.  
  16. void Dialog::on_pushButton_clicked()
  17. {
  18. ui->label->show();
  19. }
  20.  
  21. void Dialog::on_pushButton_2_clicked()
  22. {
  23. ui->label->hide();
  24. }
To copy to clipboard, switch view to plain text mode 

mythread.cpp
Qt Code:
  1. #include "mythread.h"
  2. #include "dialog.h"
  3. MyThread::MyThread(QObject *parent) :
  4. QThread(parent)
  5. {
  6. }
  7.  
  8. void MyThread::run()
  9. {
  10. exec();
  11. }
  12.  
  13. int MyThread::exec()
  14. {
  15. while(1)
  16. {
  17. monitorForAlerts();
  18. emit(testSignal("hello world!!"));
  19. sleep(1);
  20. }
  21. }
  22.  
  23. void MyThread::monitorForAlerts()
  24. {
  25. QString response = ALERTS_MESSAGE_STORAGE_PATH;
  26. QFile resp(response);
  27. resp.open(QIODevice::WriteOnly);
  28. resp.close();
  29. QFile resp1(response);
  30. char buf[121];
  31. char buf1[] = "alert1";
  32. char buf2[] = "alert2";
  33.  
  34. resp1.open(QIODevice::ReadOnly);
  35. while(resp1.size() == 0)
  36. {
  37. Sleep(3000);
  38. }
  39. qint64 lineLength = resp1.readLine(buf, sizeof(buf));
  40. resp1.close();
  41. if(strcmp(buf,buf1) == 0)
  42. {
  43. QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
  44. qDebug()<<"warning 1!!";
  45. QMessageBox::critical(this,tr("ERROR"),tr("Large change in illumination.\nPlease re-capture reference image.\n"));
  46. }
  47. if(strcmp(buf,buf2) == 0)
  48. {
  49. QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
  50. qDebug()<<"warning 2!!";
  51. QMessageBox::critical(this,tr("ERROR"),tr("The camera position has been moved or an object is obscuring its view.\nPlease check the device.\n"));
  52. }
  53. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include "dialog.h"
  2. #include <QApplication>
  3. #include "mythread.h"
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7.  
  8. MyThread mThread1;
  9. mThread1.name = "mThread1";
  10. mThread1.start();
  11.  
  12. Dialog w;
  13. w.show();
  14.  
  15. return a.exec();
  16. }
To copy to clipboard, switch view to plain text mode