forked from shelllet/cpp-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.cpp
More file actions
99 lines (76 loc) · 1.94 KB
/
Copy pathThread.cpp
File metadata and controls
99 lines (76 loc) · 1.94 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Thread.h"
#include <QThread>
#include <QFile>
#include <QDebug>
#include "WorkerThread.h"
Thread::Thread(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.pushButton, &QPushButton::clicked, this, &Thread::save);
connect(ui.pushButton_set, &QPushButton::clicked, this, &Thread::set);
connect(ui.pushButton_test, &QPushButton::clicked, this, &Thread::test);
connect(ui.pushButton_main, &QPushButton::clicked, this, &Thread::mainTest);
connect(ui.pushButton_move, &QPushButton::clicked, this, &Thread::moveToThread);
file.open(QIODevice::WriteOnly);
}
void Thread::save()
{
qDebug() << "Main thread ID: " << QThread::currentThreadId();
auto* thread = QThread::create([this]() {
//mutex.lock();
//QMutexLocker locker(&mutex);
locker.lockForWrite();
if (file.write(ui.lineEdit->text().toUtf8()) > 0) {
qDebug() << "write ok";
}
else {
qDebug() << "write failed";
}
//mutex.unlock();
locker.unlock();
qDebug() << "THREAD ID: " << QThread::currentThreadId();
QThread::sleep(3);
});
thread->start();
}
void Thread::set()
{
QThread::create([this](Thread* thread) {
for (;;)
{
save();
}
//QFile file("./setting.txt");
//file.open(QIODevice::ReadOnly);
//thread->ui.lineEdit->setText(file.readAll());
}, this)->start();
}
void Thread::test()
{
//WorkerThread* thread = new WorkerThread(this, this);
//thread->start();
file.close();
}
void Thread::mainTest()
{
WorkerThread* thread = new WorkerThread(this, this);
thread->run();
}
void Thread::moveToThread()
{
Worker* worker = new Worker(this, nullptr);
QThread* thread = new QThread(this);
worker->moveToThread(thread);
connect(thread, &QThread::started, worker, &Worker::run);
connect(worker, &Worker::finished, thread, &QThread::quit);
connect(thread, &QThread::finished, [=]() {
thread->deleteLater();
worker->deleteLater();
});
thread->start();
}
QString Thread::text() const
{
return ui.lineEdit->text();
}