Qt_IM_1 - 8BitsCoding/RobotMentor GitHub Wiki

์šฐ์„  Console Application์œผ๋กœ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

์ดํ›„ test๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑ ํ›„

๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ QObject ์ค‘ ์–ด๋– ํ•œ ๊ฒƒ์„ ์ค„๊ฒฝ์šฐ

๋ถ€๋ชจํด๋ž˜์Šค๊ฐ€ ์ข…๋ฃŒ ๋  ๊ฒฝ์šฐ ๊ทธ ์ž๋…€ ํด๋ž˜์Šค๋“ค์€ ๊ฐ™์ด ์†Œ๋ฉธ ๋  ๊ฒƒ์ด๊ฐ€?

๋ง์ด ์–ด๋ ค์šด๋ฐ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์‰ฝ๋‹ค.


// test.h
#pragma once

#include <qobject.h>
#include <qdebug.h>

class test : public QObject
{
	Q_OBJECT
public:
	explicit test(QObject* parent = nullptr);
	virtual ~test();

	void makeChild(QString name);

signals:
public slots:
};
// test.cpp
#include "test.h"

test::test(QObject* parent) : QObject(parent)
{
	qInfo() << this << "Constructed";
}

test::~test()
{
	qInfo() << this << "Deconstructed";
}

void test::makeChild(QString name)
{
	test* child = new test(this);
	child->setObjectName(name);
}

์•„๋ž˜์ฝ”๋“œ๋Š” QCoreApplication a(argc, argv);์„ test์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•˜์˜€๊ณ 

test* p๋ฅผ ๋ณ„๋„๋กœ deleteํ•˜์ง€ ์•Š์•„๋„ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋ฉธ์ด ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

#include <QtCore/QCoreApplication>
#include "test.h"

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	
	test* p = new test(&a);

	p->setObjectName("parent");

	for (int i = 0; i < 5; i++) {
		p->makeChild(QString::number(i));
	}

	return a.exec();
}

์ด๋ฏธ์ง€

์ƒ์„ฑ๊นŒ์ง€๋Š” ๋์ง€๋งŒ ์†Œ๋ฉธ์€ ์•ˆ๋จ


์•„๋ž˜์ฒ˜๋Ÿผ ํ•ด๋ณธ๋‹ค๋ฉด ์–ด๋–จ๊นŒ??

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

	test* p = new test(nullptr);

	p->setObjectName("parent");

	for (int i = 0; i < 5; i++) {
		p->makeChild(QString::number(i));
	}

	delete p;

	return a.exec();
}

์ด๋ฏธ์ง€

์†Œ๋ฉธ๊นŒ์ง€ ์™„๋ฃŒ!

์ •๋ฆฌํ•˜์ž๋ฉด QCoreApplication a(argc, argv);๋Š” ์ž๋…€ํด๋ž˜์Šค์˜ ์†Œ๋ฉธ์ž๊นŒ์ง€ ํ˜ธ์ถœํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ