zoukankan      html  css  js  c++  java
  • Qt操作Thread类

    相关资料:

    https://blog.csdn.net/u012635648/article/details/89504115

    https://blog.csdn.net/qq_28171461/article/details/90518324

    .pro

     1 QT       += core gui
     2 QT       += network
     3 
     4 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
     5 
     6 CONFIG += c++11
     7 
     8 # The following define makes your compiler emit warnings if you use
     9 # any Qt feature that has been marked deprecated (the exact warnings
    10 # depend on your compiler). Please consult the documentation of the
    11 # deprecated API in order to know how to port your code away from it.
    12 DEFINES += QT_DEPRECATED_WARNINGS
    13 
    14 # You can also make your code fail to compile if it uses deprecated APIs.
    15 # In order to do so, uncomment the following line.
    16 # You can also select to disable deprecated APIs only up to a certain version of Qt.
    17 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    18 
    19 SOURCES += 
    20     main.cpp 
    21     mainwindow.cpp
    22 
    23 HEADERS += 
    24     mainwindow.h
    25 
    26 FORMS += 
    27     mainwindow.ui
    28 
    29 # Default rules for deployment.
    30 qnx: target.path = /tmp/$${TARGET}/bin
    31 else: unix:!android: target.path = /opt/$${TARGET}/bin
    32 !isEmpty(target.path): INSTALLS += target
    View Code

    main.cpp

     1 #include "mainwindow.h"
     2 
     3 #include <QApplication>
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     QApplication a(argc, argv);
     8     MainWindow w;
     9     w.show();
    10     return a.exec();
    11 }
    View Code

    mainwindow.h

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 #include <QThread>
     6 #include <QPainter>
     7 #include <QVBoxLayout>
     8 #include <QPushButton>
     9 #include <QToolTip>
    10 #include <QDebug>
    11 #include <QTextEdit>
    12 
    13 QT_BEGIN_NAMESPACE
    14 namespace Ui { class MainWindow; }
    15 QT_END_NAMESPACE
    16 
    17 
    18 class WorkThread : public QThread
    19 {
    20 
    21 public:
    22     WorkThread(QTextEdit *textEdit, QObject *parent = nullptr);
    23     ~WorkThread();
    24     // 结束线程
    25     void stop();
    26     void paused();
    27     void begin();
    28 protected:
    29     bool m_stop;
    30     bool m_paused;
    31     QTextEdit *m_pTextEdit;
    32     void run();
    33 };
    34 
    35 class MainWindow : public QMainWindow
    36 {
    37     Q_OBJECT
    38 
    39 public:
    40     MainWindow(QWidget *parent = nullptr);
    41     ~MainWindow();
    42 private slots:
    43     void on_pushButton_clicked();
    44 
    45     void on_pushButton_2_clicked();
    46 
    47     void on_pushButton_3_clicked();
    48 
    49 private:
    50     Ui::MainWindow *ui;
    51     WorkThread *m_pWorkThread;
    52 };
    53 #endif // MAINWINDOW_H
    View Code

    mainwindow.cpp

     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 
     4 MainWindow::MainWindow(QWidget *parent)
     5     : QMainWindow(parent)
     6     , ui(new Ui::MainWindow)
     7 {
     8     ui->setupUi(this);
     9     setWindowTitle(QStringLiteral("Qt操作Thread类"));
    10 
    11     m_pWorkThread = new WorkThread(ui->textEdit);
    12     m_pWorkThread->start();
    13 }
    14 
    15 MainWindow::~MainWindow()
    16 {
    17     delete ui;
    18 
    19     m_pWorkThread->stop();
    20     m_pWorkThread->exit(0);
    21     delete m_pWorkThread;
    22 }
    23 
    24 void WorkThread::run()
    25 {
    26     while(!m_stop)
    27     {
    28         if (false == m_paused)
    29         {
    30             m_pTextEdit->append(QStringLiteral("进行中..."));
    31         }
    32         sleep(2);
    33     }
    34 }
    35 
    36 WorkThread::WorkThread(QTextEdit *textEdit, QObject *parent)
    37     :QThread(parent)
    38 {
    39     m_stop = false;
    40     m_paused = false;
    41     m_pTextEdit = textEdit;
    42 }
    43 
    44 WorkThread::~WorkThread()
    45 {
    46     // 请求终止
    47     requestInterruption();
    48     quit();
    49     wait();
    50 }
    51 
    52 void WorkThread::stop()
    53 {
    54     m_stop = true;
    55 }
    56 
    57 void WorkThread::paused()
    58 {
    59     m_paused = true;
    60 }
    61 
    62 void WorkThread::begin()
    63 {
    64     m_paused = false;
    65 }
    66 
    67 void MainWindow::on_pushButton_clicked()
    68 {
    69     m_pWorkThread->begin();
    70 }
    71 
    72 void MainWindow::on_pushButton_2_clicked()
    73 {
    74     m_pWorkThread->paused();
    75 }
    76 
    77 void MainWindow::on_pushButton_3_clicked()
    78 {
    79     m_pWorkThread->stop();
    80 }
    View Code
  • 相关阅读:
    C嵌入汇编
    App 运营 推广相关
    POJ 3904 Sky Code
    数组的复制与动态扩展算法
    另类病毒的自删除方法
    oracle触发器中增删改查本表
    POJ 2773 Happy 2006 数学题
    Android手机便携式wifi的使用及无线数据传输(主要针对XP系统)
    Find the minimum线段树成段更新
    使用visual c++ 2005编译64位可执行文件
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/13405296.html
Copyright © 2011-2022 走看看