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!");
}

์ด๋ฏธ์ง€

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