QThread QT线程使用案例
来源:互联网 发布:办公室网络维护 编辑:程序博客网 时间:2024/05/16 17:34
1.工程文件一共5个,创建2个线程,同时主界面一个按钮可以点击。
分别是 main.cpp, mainwindow.cpp, mainwindow.h, mythread.cpp, mythread.h .
2.
1).main.cpp代码如下:
#include "mainwindow.h"#include <QApplication>#include "mythread.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); Mythread tha; Mythread thb; tha.start(); thb.start(); return a.exec();}
2).mianwindow.cpp代码如下:
#include "mainwindow.h"#include "ui_mainwindow.h"#include "mythread.h"#include <QDebug>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_pushButton_clicked(){ qDebug()<<"on_pushButton_clicked";}
3).mainwindow.h代码如下:
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked();private: Ui::MainWindow *ui;};#endif // MAINWINDOW_H
4).mythread.cpp代码如下:
#include "mythread.h"#include <QDebug>#include <QString>Mythread::Mythread(QObject *parent) : QThread(parent){}void Mythread::run(){ for(int count=0; count<20; count++) { sleep(1); qDebug()<<"Ping+"<<this->currentThreadId()<<count+1; }}
5).mythead.h 代码如下:
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>class Mythread : public QThread{ Q_OBJECTpublic: explicit Mythread(QObject *parent = 0); signals: public slots:public : virtual void run();};#endif // MYTHREAD_H
3.运行结果:
1 0
- QThread QT线程使用案例
- Qt::QThread->线程的使用
- Qt 中线程QThread的简单使用
- QT线程QThread的简单使用实例
- Qt线程(QThread)的使用/简介
- qt线程类---QThread
- Qt线程QThread简析
- Qt线程(QThread)
- QT中的线程QThread
- Qt之线程QThread
- Qt中利用QThread在线程中同步使用QFtp
- qt线程 qthread(转)
- QT——QThread线程
- Qt之线程(QThread)
- Qt之线程(QThread)
- Qt之多线程QThread简介
- Qt之线程(QThread)
- QThread线程使用
- spark学习14之使用maven快速切换本地调试的spark版本
- 设计模式 —— 装饰器模式(Decorator Pattern)
- svn使用难点相关资料
- Objective-C中关于请求返回NSData数据解析成NSDictionary或NSArray的方法
- 【Leetcode】之Minimum Path Sum
- QThread QT线程使用案例
- 前端开发学习指南
- Javascript(加快脚本加载速度的方法:压缩脚本文件。)
- PPT演讲者需要注意的事项
- Drawable Animation
- Android N 多窗口布局 省电模式 全新通知功能
- Android 检查更新版本(带有通知栏,进度条,Notification)
- 欢迎使用CSDN-markdown编辑器
- 羞羞的成人漫画笑话,18岁以下不要随意观看哦