Hi, All

I do a testing which use a work thread to access a variable (here is str) which belong to main thread. I found if I add QueuedConnection in connect function ,it will not crash,but without it will crash .

Does anybody know why?

Thanks advance for your help.

Qt Code:
  1. #ifndef AAA_
  2. #define AAA_
  3. #include <QThread>
  4. #include <QString>
  5. #include <QDebug>
  6. class myClass : public QThread
  7. {
  8. Q_OBJECT
  9. public:
  10. myClass ()
  11. {
  12. connect(this,
  13. SIGNAL(startSignal()),
  14. this, SLOT(onDoComm())); //will crash
  15. //,Qt::QueuedConnection); //add this parameter will not
  16. }
  17. void run() { exec(); }
  18. void startComm() {emit startSignal();}
  19. signals:
  20. void startSignal();
  21. public slots:
  22. void onDoComm()
  23. {
  24. static long i=1;
  25. qDebug()<<str;
  26. str=QString("%1%2").arg(i++);
  27. emit startSignal();
  28. };
  29. private:
  30. QString str;
  31. };
  32.  
  33.  
  34. #endif
  35.  
  36.  
  37.  
  38. #include <iostream>
  39. #include <QApplication>
  40. #include "aaa.h"
  41.  
  42. using namespace std;
  43. int main(int argc ,char *argv[])
  44. {
  45. QApplication app(argc, argv);
  46.  
  47. myClass a;
  48. a.start();
  49. a.startComm();
  50. app.exec();
  51. }
To copy to clipboard, switch view to plain text mode