Qt_7 - 8BitsCoding/RobotMentor GitHub Wiki
#include <QtCore/qdebug.h>
qDebug() << "MyDialog constructor called" << endl;
qDebug() << "MyDialog destuctor called" << endl;
main ๋ฌธ์ด ๋ค์๊ณผ ๊ฐ์๋
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyDialog* dialog = new MyDialog;
QObject::connect(dialog, SIGNAL(onKeywordMatched(const QString&)), &app, SLOT(quit()));
dialog->show();
return app.exec();
}
MyDialog* dialog = new MyDialog;
๋ ์ธ์ ์ญ์ ๋๋๊ฐ? ํ๋ก๊ทธ๋จ์ด ์ฃฝ์๋??
MyDialog::~MyDialog()
{
qDebug() << "MyDialog destuctor called" << endl;
}
์์ ๊ฐ์ด ์๋ฉธ์์ qDebug๋ฅผ ๋ฃ์ด๋ณด๋ฉด ์์ง๋ง, ํ๋ก๊ทธ๋จ์ด ์ฃฝ์๋ ์๋ฉธ์๊ฐ ๊ฐ์ด ํธ์ถ๋์ง ์๋๋ค???
์ด๋ป๊ฒ ํด์ผํ ๊น??
MyDialog dialog;
์คํ ์ ์์ฑํ๊ธฐ
์ผ๋ฐ์ ์ผ๋ก QObject์์ ์์ฑํ ํด๋์ค๋ค์ Heap์ ์๊ธฐ๋ฅผ ์ถ์ฒํ๋ค.
์ฐ์ mydialog์์ ์ฌ์ฉํ mywidget์ ์์ฑํ๋ค.
#pragma once
#include <qwidget.h>
#include <QtWidgets/QPushButton.h>
class mywidget : public QWidget
{
Q_OBJECT
public:
explicit mywidget(QWidget* parent = 0);
virtual ~mywidget();
QPushButton* button;
signals:
public slots:
};
#include "mywidget.h"
#include <iostream>
#include <QtCore/qdebug.h>
#include <QtWidgets/qlayout.h>
using namespace std;
mywidget::mywidget(QWidget* parent) : QWidget(parent)
{
qDebug() << "mywidget constructor called" << endl;
button = new QPushButton;
QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(button);
setLayout(layout);
}
mywidget::~mywidget()
{
qDebug() << "mywidget destructor called" << endl;
}
mydialog์ ์ถ๊ฐํ๋ค.
// mydialog.h
class QLineEdit;
class mywidget;
class MyDialog : public QDialog{
Q_OBJECT
public:
explicit MyDialog(QDialog* parent = nullptr);
virtual ~MyDialog();
QLineEdit* lineEdit;
mywidget* widget;
// mydialog.cpp
#include <mywidget.h>
using namespace std;
MyDialog::MyDialog(QDialog* parent) : QDialog(parent) {
qDebug() << "MyDialog constructor called" << endl;
lineEdit = new QLineEdit;
widget = new mywidget;
QObject::connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(myTextChangd(const QString&)));
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(lineEdit);
layout->addWidget(widget);
setLayout(layout);
}
์ด๋ ๊ฒ ํด์ ํ๋ก๊ทธ๋จ์ ์ข ๋ฃํ ๊ฒฝ์ฐ mywidget์ ์๋ฉธ์ ๊น์ง ํธ์ถ๋จ์ ํ์ธํ ์ ์๋ค.