Qt多线程, 简单Demo

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

 public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void uiToOther();
    static QString info;
    static QString name1;
    static QString name2;
    void go();

 private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();

 private:
    Ui::MainWindow *ui;
};
#endif  // MAINWINDOW_H

#ifndef THREAD_H
#define THREAD_H
#include <QApplication>
#include <QDebug>
#include <QThread>
#include "mainwindow.h"
#include "ui_mainwindow.h"

class Thread : public QThread {
 public:
    void run() {
        while (!stopped) {
            qDebug() << MainWindow::info << "线程启动";
            QThread::msleep(400);
        }
    }
    void stop() {
        stopped = true;
    }

 private:
    volatile bool stopped;
};
#endif  // THREAD_H

#include <QApplication>
#include "mainwindow.h"
#include "thread.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
#include "mainwindow.h"
#include "thread.h"
#include "ui_mainwindow.h"

QString MainWindow::info = "";
static Thread *thread1   = new Thread();
static Thread *thread2   = new Thread();
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);
    info = ui->lineEdit->text();
    go();
}

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

void MainWindow::go() {
    thread1->start();
    thread2->start();
}

void MainWindow::on_pushButton_clicked() {
    thread1->stop();
}

void MainWindow::on_pushButton_2_clicked() {
    thread2->stop();
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>383</width>
    <height>233</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>40</y>
      <width>80</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>停止进程1</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>90</y>
      <width>80</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>停止进程2</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>40</y>
      <width>221</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>one</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值