Posted By

yesraaj on 02/09/10


Tagged

Qt signalslot QThread


Versions (?)

Qt signaling across threads.


 / Published in: C++
 

URL: Qt signaling across threads.

  1. //main.cpp//
  2. #include <QtGui/QApplication>
  3. #include <QWidget>
  4. #include <QHBoxLayout>
  5. #include <QLineEdit>
  6. #include <QPushButton>
  7. #include "thread.h"
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. QApplication a(argc, argv);
  12. QWidget widget;
  13. //GUI
  14. QHBoxLayout layout;
  15. QLineEdit *line;
  16. QPushButton *button;
  17. layout.addWidget(line = new QLineEdit("Default string",&widget));
  18. layout.addWidget(button = new QPushButton("Increment",&widget));
  19. widget.setLayout(&layout);
  20. //Thread creation
  21. Thread thread;
  22. thread.start();
  23. qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
  24. //Connections
  25. QObject::connect(button,SIGNAL(clicked()),&thread,SIGNAL(updateObject()));
  26. QObject::connect(&thread,SIGNAL(updateLineEdit(QString)),line,SLOT(setText(QString)));
  27. widget.show();
  28. return a.exec();
  29. }
  30.  
  31. //thread.h//
  32. #ifndef THREAD_H
  33. #define THREAD_H
  34.  
  35. #include <QThread>
  36. #include "object.h"
  37.  
  38. class Thread : public QThread
  39. {
  40. Q_OBJECT
  41. public:
  42. Thread();
  43. ~Thread();
  44. public slots:
  45. void emitUpdateObject();
  46. void emitUpdateLineEdit(QString);
  47. signals:
  48. void updateObject();
  49. void updateLineEdit(QString);
  50. private:
  51. Object object;
  52. protected:
  53. void run();
  54. };
  55.  
  56. #endif // THREAD_H
  57.  
  58. //thread.cpp//
  59. #include "thread.h"
  60.  
  61. Thread::Thread()
  62. {
  63. object.moveToThread(this);
  64. connect(this,SIGNAL(updateObject()),&object,SLOT(doTimeConsumingWork()));//Queued connection
  65. connect(&object,SIGNAL(finishedComplexWork(QString)),this,SLOT(emitUpdateLineEdit(QString)));
  66. }
  67. void Thread::emitUpdateObject(){
  68. emit updateObject();
  69. }
  70. void Thread::emitUpdateLineEdit(QString str){
  71. emit updateLineEdit(str);
  72. }
  73. void Thread::run(){
  74. exec();
  75. }
  76. Thread::~Thread(){
  77. quit();
  78. wait();
  79. }
  80.  
  81. //object.h//
  82. #ifndef OBJECT_H
  83. #define OBJECT_H
  84.  
  85. #include <QObject>
  86. #include <QMutex>
  87. class Object : public QObject
  88. {
  89. Q_OBJECT
  90. public:
  91. explicit Object(QObject *parent = 0);
  92.  
  93. signals:
  94. void finishedComplexWork(QString);
  95.  
  96. public slots:
  97. void doTimeConsumingWork();
  98. private:
  99. QMutex mutex;
  100.  
  101. };
  102.  
  103. #endif // OBJECT_H
  104.  
  105. //object.cpp//
  106. #include "object.h"
  107. #include <QDebug>
  108. #include <QThread>
  109.  
  110. Object::Object(QObject *parent) :
  111. QObject(parent)
  112. {
  113. }
  114. void Object::doTimeConsumingWork(){
  115. for(unsigned int i = 0;i<10000;++i){
  116. //do nothing
  117. qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
  118.  
  119. }
  120.  
  121. static int a = 1;
  122. QMutexLocker guard(&mutex);
  123. ++a;
  124. QString txt("Modified");
  125. txt += QString::number(a);
  126. emit finishedComplexWork(txt);
  127. }

Report this snippet  

You need to login to post a comment.