qt多线程一个简单的例子
2014-03-12 13:29
411 查看
作者:zgrjkflmkyc
转自:/article/8178732.html
代码如下:
[cpp]
view plaincopy
/**********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();
}
[cpp]
view plaincopy
/************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
[cpp]
view plaincopy
/*************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;
}
[cpp]
view plaincopy
/***************MyThread.h******************/
#include <QtCore/QThread>
#include <QtGui/QPushButton>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QPushButton *pb);
~MyThread();
QPushButton *pb;
protected:
void run();
};
[cpp]
view plaincopy
/**************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文件内容:
[plain]
view plaincopy
######################################################################
# 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片段:
[plain]
view plaincopy
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
运行效果截图:
(仅供参考)
转自:/article/8178732.html
代码如下:
[cpp]
view plaincopy
/**********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();
}
[cpp]
view plaincopy
/************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
[cpp]
view plaincopy
/*************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;
}
[cpp]
view plaincopy
/***************MyThread.h******************/
#include <QtCore/QThread>
#include <QtGui/QPushButton>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QPushButton *pb);
~MyThread();
QPushButton *pb;
protected:
void run();
};
[cpp]
view plaincopy
/**************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文件内容:
[plain]
view plaincopy
######################################################################
# 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片段:
[plain]
view plaincopy
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
运行效果截图:
(仅供参考)
相关文章推荐
- [Qt]一个简单的Qt Widget多线程处理图像的例子
- Qt学习 之 Socket通信(一个简单的例子)
- 一个简单的多线程服务器例子
- QT 自定义消息(超级简单的一个例子)
- 一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程
- 一个简单的java多线程例子
- Qt实现多线程的简单例子(VS2015Professional+Qt5.7.1)
- 一个不错的多线程Socket服务器简单例子
- [转]一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程
- 一个简单的生产者与消费者的多线程例子(一):简单的框架
- C# 一个简单的多线程例子
- 一个简单的多线程例子
- Qt学习 之 Socket通信(一个简单的例子)
- 一个简单的生产者与消费者的多线程例子(二):不让消费者或生产者无限期地等待
- qt qml和c++交互的一个简单例子
- c#中子线程控制进度条的一个简单例子(多线程问题)
- qt4:一个简单而完整的例子
- 一个关于多线程的简单例子(两种实现方法)
- 一个简单的多线程例子-临界区
- 多线程死锁的一个简单例子