qt多线程一个简单的例子

44 篇文章 4 订阅

代码如下:

/**********Main.cpp*************/

#include <QtGui/QApplication>
#include "MainWindow.h"

int main(int argc,char *argv[]){
   QApplication a(argc,argv);
   MainWindow window;
   window.show();
   return a.exec();
}

/************MainWindow.h**************/

#ifndef MINWINDOW_H_
#define MINWINDOW_H_

#include <QtGui/QWidget>
#include "MyThread.h"
#include <QtGui/QPushButton>

class MainWindow : public QWidget
{
public:
      MainWindow(QWidget *parent = 0);
      ~MainWindow();
      MyThread *thread;
      QPushButton *pb;
};

#endif

/*************MainWindow.cpp****************/

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
   this->setGeometry(0,0,300,300);
   pb=new QPushButton("AA",this);
   pb->setGeometry(0,0,30,30);
   thread=new MyThread(pb);
   thread->start();
}

MainWindow::~MainWindow()
{
  delete thread;
}

/***************MyThread.h******************/

#include <QtCore/QThread>
#include <QtGui/QPushButton>

class MyThread : public QThread
{
     Q_OBJECT
public:
      MyThread(QPushButton *pb);
      ~MyThread();
      QPushButton *pb;
protected:
      void run();
};

/**************MyThread.cpp*****************/

#include "MyThread.h"
#include <iostream>

MyThread::MyThread(QPushButton *pb) : QThread()
{
  this->pb=pb;
}

void MyThread::run(){
    int i=1;
    while(true){
       std::cout<<"the number is:"<<i<<std::endl;
       QThread::msleep(1000);
       i+=10;
       pb->move(i,i);
    }
}

MyThread::~MyThread()
{

}

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

######################################################################
# Automatically generated by qmake (2.01a) ?? 6? 15 23:36:31 2013
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += MainWindow.h MyThread.h
SOURCES += Main.cpp MainWindow.cpp MyThread.cpp

编译前最终的Makefile片段:

CC            = gcc
CXX           = g++
DEFINES       = -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
CFLAGS        = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS      = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
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.
LINK          = g++
LFLAGS        = -Wl,-O1
LIBS          = $(SUBLIBS)  -L/usr/lib/i386-linux-gnu -lQtGui -lQtCore -lpthread 
AR            = ar cqs
RANLIB        = 
QMAKE         = /usr/bin/qmake
TAR           = tar -cf
COMPRESS      = gzip -9f
COPY          = cp -f
SED           = sed
COPY_FILE     = $(COPY)
COPY_DIR      = $(COPY) -r
STRIP         = strip
INSTALL_FILE  = install -m 644 -p
INSTALL_DIR   = $(COPY_DIR)
INSTALL_PROGRAM = install -m 755 -p
DEL_FILE      = rm -f
SYMLINK       = ln -f -s
DEL_DIR       = rmdir
MOVE          = mv -f
CHK_DIR_EXISTS= test -d
MKDIR         = mkdir -p

运行效果截图:

(仅供参考)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值