Qt_IM_2 - 8BitsCoding/RobotMentor GitHub Wiki
์๋์ ๊ฐ์ด
QPointer
๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ ๋ง ๊ฐ์๋ฐ์ด๋ค ์ฅ์ ์ด ์๋์ง๋ ์...
๊ตณ์ด ์ฐพ์๋ฉด delete๋ฅผ ์ ์ธ๋ Object์์ ํด์ค์ผ ํ๋ค์ ๋?
// main.cpp
#include <QtCore/QCoreApplication>
#include <qdebug.h>
#include <qpointer.h>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QObject* obj = new QObject(nullptr);
obj->setObjectName("My Object");
QPointer<QObject> p(obj);
test t;
t.widget = p;
t.useWidget();
if (p.data()) qInfo() << p.data();
delete obj;
t.useWidget();
return a.exec();
}
// test.h
#pragma once
#include <qobject.h>
#include <qdebug.h>
#include <qpointer.h>
class test : public QObject
{
Q_OBJECT
public:
explicit test(QObject * parent = nullptr);
QPointer<QObject> widget;
void useWidget();
signals:
public slots:
};
// test.cpp
#include "test.h"
test::test(QObject* parent) : QObject(parent)
{
};
void test::useWidget()
{
// see if there is a pointer!
if (!widget.data()) {
qInfo() << "No pointer!!";
return;
}
qInfo() << "Widget : " << widget.data();
// Access the pointer
widget.data()->setObjectName("used Widget!");
}