Qt 多线程例子

转载自:http://blog.csdn.net/zgrjkflmkyc/article/details/9270903

代码如下:

[cpp]  view plain  copy
  1. /**********Main.cpp*************/  
  2.   
  3. #include <QtGui/QApplication>  
  4. #include "MainWindow.h"  
  5.   
  6. int main(int argc,char *argv[]){  
  7.    QApplication a(argc,argv);  
  8.    MainWindow window;  
  9.    window.show();  
  10.    return a.exec();  
  11. }  

[cpp]  view plain  copy
  1. /************MainWindow.h**************/  
  2.   
  3. #ifndef MINWINDOW_H_  
  4. #define MINWINDOW_H_  
  5.   
  6. #include <QtGui/QWidget>  
  7. #include "MyThread.h"  
  8. #include <QtGui/QPushButton>  
  9.   
  10. class MainWindow : public QWidget  
  11. {  
  12. public:  
  13.       MainWindow(QWidget *parent = 0);  
  14.       ~MainWindow();  
  15.       MyThread *thread;  
  16.       QPushButton *pb;  
  17. };  
  18.   
  19. #endif  

[cpp]  view plain  copy
  1. /*************MainWindow.cpp****************/  
  2.   
  3. #include "MainWindow.h"  
  4.   
  5. MainWindow::MainWindow(QWidget *parent) : QWidget(parent)  
  6. {  
  7.    this->setGeometry(0,0,300,300);  
  8.    pb=new QPushButton("AA",this);  
  9.    pb->setGeometry(0,0,30,30);  
  10.    thread=new MyThread(pb);  
  11.    thread->start();  
  12. }  
  13.   
  14. MainWindow::~MainWindow()  
  15. {  
  16.   delete thread;  
  17. }  

[cpp]  view plain  copy
  1. /***************MyThread.h******************/  
  2.   
  3. #include <QtCore/QThread>  
  4. #include <QtGui/QPushButton>  
  5.   
  6. class MyThread : public QThread  
  7. {  
  8.      Q_OBJECT  
  9. public:  
  10.       MyThread(QPushButton *pb);  
  11.       ~MyThread();  
  12.       QPushButton *pb;  
  13. protected:  
  14.       void run();  
  15. };  

[cpp]  view plain  copy
  1. /**************MyThread.cpp*****************/  
  2.   
  3. #include "MyThread.h"  
  4. #include <iostream>  
  5.   
  6. MyThread::MyThread(QPushButton *pb) : QThread()  
  7. {  
  8.   this->pb=pb;  
  9. }  
  10.   
  11. void MyThread::run(){  
  12.     int i=1;  
  13.     while(true){  
  14.        std::cout<<"the number is:"<<i<<std::endl;  
  15.        QThread::msleep(1000);  
  16.        i+=10;  
  17.        pb->move(i,i);  
  18.     }  
  19. }  
  20.   
  21. MyThread::~MyThread()  
  22. {  
  23.   
  24. }  

编译前最终的.pro文件内容:

[plain]  view plain  copy
  1. ######################################################################  
  2. # Automatically generated by qmake (2.01a) ?? 6? 15 23:36:31 2013  
  3. ######################################################################  
  4.   
  5. TEMPLATE = app  
  6. TARGET =   
  7. DEPENDPATH += .  
  8. INCLUDEPATH += .  
  9.   
  10. # Input  
  11. HEADERS += MainWindow.h MyThread.h  
  12. SOURCES += Main.cpp MainWindow.cpp MyThread.cpp  

编译前最终的Makefile片段:

[plain]  view plain  copy
  1. CC            = gcc  
  2. CXX           = g++  
  3. DEFINES       = -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED  
  4. CFLAGS        = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)  
  5. CXXFLAGS      = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)  
  6. INCPATH       = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I.  
  7. LINK          = g++  
  8. LFLAGS        = -Wl,-O1  
  9. LIBS          = $(SUBLIBS)  -L/usr/lib/i386-linux-gnu -lQtGui -lQtCore -lpthread   
  10. AR            = ar cqs  
  11. RANLIB        =   
  12. QMAKE         = /usr/bin/qmake  
  13. TAR           = tar -cf  
  14. COMPRESS      = gzip -9f  
  15. COPY          = cp -f  
  16. SED           = sed  
  17. COPY_FILE     = $(COPY)  
  18. COPY_DIR      = $(COPY) -r  
  19. STRIP         = strip  
  20. INSTALL_FILE  = install -m 644 -p  
  21. INSTALL_DIR   = $(COPY_DIR)  
  22. INSTALL_PROGRAM = install -m 755 -p  
  23. DEL_FILE      = rm -f  
  24. SYMLINK       = ln -f -s  
  25. DEL_DIR       = rmdir  
  26. MOVE          = mv -f  
  27. CHK_DIR_EXISTS= test -d  
  28. MKDIR         = mkdir -p  

运行效果截图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值