#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>