分类: LINUX
2012-09-21 16:27:37
1. mythread.h
2. #ifndef MYTHREAD_H
3. #define MYTHREAD_H
4. #include
5. class MyThread : public QThread
6. {
7. Q_OBJECT
8. public:
9. MyThread();
10. ~MyThread();
11. protected:
12. void run();
13. signals:
14. void changeText(QString str);
15. };
16. #endif // MYTHREAD_H
17. widgett.h
18. #ifndef WIDGETT_H
19. #define WIDGETT_H
20. #include
21. #include "ui_widgett.h"
22. class WidgetT : public QMainWindow
23. {
24. Q_OBJECT
25. public:
26. WidgetT(QWidget *parent = 0, Qt::WFlags flags = 0);
27. ~WidgetT();
28. private:
29. Ui::WidgetTClass ui;
30. private slots:
31. void labelSetText(QString qstr);
32. };
33. #endif // WIDGETT_H
34. mythread.cpp
35. #include "mythread.h"
36. MyThread::MyThread()
37. : QThread()
38. {
39. }
40. MyThread::~MyThread()
41. {
42. }
43. void MyThread::run(){
44. static int i=0;
45. while(true)
46. {
47. ++i;
48. QString strnum = QString::number(i);
49. emit changeText(strnum);
50.
51. QThread::sleep(1);
52. }
53. }
54. widgett.cpp
55. #include "widgett.h"
56. #include "mythread.h"
57. Q_DECLARE_METATYPE(QString);
58. WidgetT::WidgetT(QWidget *parent, Qt::WFlags flags)
59. : QMainWindow(parent, flags)
60. {
61. ui.setupUi(this);
62. MyThread *mythread = new MyThread;
63. int id=qRegisterMetaType
64. connect(mythread,SIGNAL(changeText(QString)),this,SLOT(labelSetText(QString)),Qt::QueuedConnection);
65. mythread->start();
66. }
67. WidgetT::~WidgetT()
68. {
69. }
70. void WidgetT::labelSetText(QString qstr){
71. ui.label->setText(qstr);
72. }