Qt_Core_AD_2 - 8BitsCoding/RobotMentor GitHub Wiki


// main.cpp
#include <QtCore/QCoreApplication>

#include <qthread.h>
#include <qdebug.h>
#include <qtimer.h>

#include "test.h"

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	qInfo() << "App Thread: " << a.thread();
	qInfo() << "Current Thread: " << QThread::currentThread();

	test t;
	qInfo() << "Timer Thread: " << t.thread();
	// App과 같은 thread아래 돌아간다.

	QThread thread;
	t.moveToThread(&thread);
	// test t(&a);
	// test가 App의 자녀라면 thread를 옮길 수 없다.(에러가 발생)
	qInfo() << "Timer Thread(moved): " << t.thread();

	t.start();

	qInfo() << "Thread State: " << thread.isRunning();
	// 이대로 실해아면 thread가 죽어있다.(false)

	thread.start();
	// thread를 실행하는 절차가 필요하다.

	return a.exec();
}
// test.h
#pragma once

#include <QObject>
#include <qthread.h>
#include <qdebug.h>
#include <qtimer.h>
#include <qdatetime.h>

class test : public QObject
{
	Q_OBJECT

public:
	explicit test(QObject *parent = nullptr);
	~test();

signals:

public slots:
	void timeout();
	void start();

private:
	QTimer timer;
};
// test.cpp
#include "test.h"

test::test(QObject *parent)
	: QObject(parent)
{
}

test::~test()
{
}

void test::timeout()
{
	qInfo() << QDateTime::currentDateTime().toString();
}

void test::start()
{
	connect(&timer, &QTimer::timeout, this, &test::timeout);
	timer.setInterval(1000);
	timer.start();
}

이미지


#include "test.h"

test::test(QObject *parent)
	: QObject(parent)
{
}

test::~test()
{
}

void test::timeout()
{
	qInfo() << QDateTime::currentDateTime().toString() << " on " << QThread::currentThread();
}

void test::start()
{
	connect(&timer, &QTimer::timeout, this, &test::timeout);
	timer.setInterval(1000);
	timer.start();
}

이미지


#include <QtCore/QCoreApplication>

#include <qthread.h>
#include <qdebug.h>
#include <qtimer.h>

#include "test.h"

static QSharedPointer<QThread> sptr;
void timeout()
{
	if (!sptr.isNull())
	{
		qInfo() << "Time out - stopping thread from" << QThread::currentThread();;
		sptr.data()->quit();
	}
}

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	qInfo() << "App Thread: " << a.thread();
	qInfo() << "Current Thread: " << QThread::currentThread();

	test t;
	qInfo() << "Timer Thread: " << t.thread();
	// App과 같은 thread아래 돌아간다.

	QThread thread;
	sptr.reset(&thread);
	t.moveToThread(&thread);
	// test t(&a);
	// test가 App의 자녀라면 thread를 옮길 수 없다.(에러가 발생)
	qInfo() << "Timer Thread(moved): " << t.thread();

	t.start();

	qInfo() << "Thread State: " << thread.isRunning();
	// 이대로 실해아면 thread가 죽어있다.(false)

	thread.start();
	// thread를 실행하는 절차가 필요하다.

	QTimer timer;
	timer.singleShot(5000, &timeout);

	return a.exec();
}

이미지

⚠️ **GitHub.com Fallback** ⚠️