-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
53 lines (46 loc) · 2.24 KB
/
mainwindow.cpp
File metadata and controls
53 lines (46 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Запуск выполнения метода run будет осуществляться по сигналу запуска от соответствующего потока
connect(&thread_1, &QThread::started, &exampleObject_1, &ExampleObject::run);
connect(&thread_2, &QThread::started, &exampleObject_2, &ExampleObject::run);
// Остановка потока же будет выполняться по сигналу finished от соответствующего объекта в потоке
connect(&exampleObject_1, &ExampleObject::finished, &thread_1, &QThread::quit); //NOT terminate
connect(&exampleObject_2, &ExampleObject::finished, &thread_2, &QThread::quit); //NOT terminate
// коннект для передачи данных из первого объекта в первом потоке, ко второму объекту во втором потоке
connect(&exampleObject_1, &ExampleObject::sendMessage, &exampleObject_2, &ExampleObject::setMessage_2, Qt::DirectConnection);
exampleObject_1.moveToThread(&thread_1); // Передаём объекты в соответствующие потоки
exampleObject_2.moveToThread(&thread_2);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_write_1_clicked()
{
// Устанавливаем текст в первый объект в первом потоке
exampleObject_1.setMessage(ui->lineEdit_1->text());
}
void MainWindow::on_write_2_clicked()
{
// Устанавливаем текст во второй объект во втором потоке
exampleObject_2.setMessage(ui->lineEdit_2->text());
}
void MainWindow::on_start_clicked()
{
// Запуск потоков
exampleObject_1.setRunning(true);
exampleObject_2.setRunning(true);
thread_1.start();
thread_2.start();
}
void MainWindow::on_stop_clicked()
{
// Остановка потоков через завершение выполнения методов run в объектах
exampleObject_1.setRunning(false);
exampleObject_2.setRunning(false);
}