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);
๋ ์๋ ํด๋์ค์ ์๋ฉธ์๊น์ง ํธ์ถํด์ฃผ์ง ์๋๋ค.