이 페이지에서

QPointer Class

template <typename T> class QPointer

QPointer 클래스는 ` QObject`에 대한 가드된 포인터를 제공하는 템플릿 클래스입니다. 더 보기...

헤더: #include <QPointer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

QPointer 비교

범주비교 가능한 유형설명
동등성QPointer
동등성QPointer<X>, X* 및 std::nullptr_t.여기서 X와 T는 호환되는 유형으로, (cv- 한정자를 제외하고) 동일하거나, 하나가 다른 하나의 기본 유형인 경우를 의미합니다.

공개 함수

QPointer()
(since 6.6) QPointer(QPointer<X> &&other)
QPointer(T *p)
(since 6.6) QPointer(const QPointer<X> &other)
QPointer(std::nullptr_t)
~QPointer()
void clear()
T *data() const
(since 6.0) T *get() const
bool isNull() const
void swap(QPointer<T> &other)
operator T *() const
T &operator*() const
T *operator->() const
(since 6.6.1) QPointer<T> &operator=(QPointer<X> &&other)
QPointer<T> &operator=(T *p)
(since 6.6) QPointer<T> &operator=(const QPointer<X> &other)
bool operator!=(X *const &lhs, const QPointer<T> &rhs)
bool operator!=(const QPointer<T> &lhs, X *const &rhs)
bool operator!=(const QPointer<T> &lhs, const QPointer<X> &rhs)
bool operator!=(const QPointer<T> &lhs, const std::nullptr_t &rhs)
bool operator!=(const std::nullptr_t &lhs, const QPointer<T> &rhs)
bool operator==(X *const &lhs, const QPointer<T> &rhs)
bool operator==(const QPointer<T> &lhs, X *const &rhs)
bool operator==(const QPointer<T> &lhs, const QPointer<X> &rhs)
bool operator==(const QPointer<T> &lhs, const std::nullptr_t &rhs)
bool operator==(const std::nullptr_t &lhs, const QPointer<T> &rhs)

자세한 설명

가드된 포인터인 QPointer<T>는 일반 C++ 포인터 T * 와 비슷하게 동작하지만, 참조되는 객체가 소멸될 때 자동으로 지워진다는 점이 다릅니다(이러한 경우 일반 C++ 포인터는 "달링 포인터"가 됩니다). 템플릿 매개변수 T 는 포인터가 가리키는 QObject 에서 파생된 유형을 지정하며, T 는 QObject 의 하위 클래스여야 합니다.

가드된 포인터(Guarded pointers)는 다른 주체가 소유하고 있는 객체( QObject )에 대한 포인터를 저장해야 할 때 유용하며, 이 경우 사용자가 여전히 해당 포인터에 대한 참조를 보유하고 있는 동안에도 객체가 소멸될 수 있습니다. 이 포인터의 유효성을 안전하게 확인할 수 있습니다.

Qt 5에서는 QPointer 사용 시 동작에 약간의 변경 사항이 도입되었음을 유의하십시오.

  • QWidget (또는 QWidget 의 하위 클래스)에 QPointer를 사용할 때, 이전에는 QWidget 의 소멸자에 의해 QPointer가 초기화되었습니다. 이제 QPointer는 QObject 의 소멸자에 의해 초기화됩니다( QWeakPointer 객체가 이 시점에 초기화되기 때문입니다). 위젯을 추적하는 모든 QPointer는 QWidget 의 소멸자가 추적 중인 위젯의 자식 객체들을 소멸시키기 전까지는 초기화되지 않습니다.

Qt는 또한 참조 카운팅이 적용된 공유 포인터 객체의 구현체인 ` QSharedPointer`를 제공하며, 이를 사용하여 개별 포인터에 대한 참조 모음을 관리할 수 있습니다.

예시:

    QPointer<QLabel> label = new QLabel;
    label->setText("&Status:");
    ...
    if (label)
        label->show();

만약 그 사이에 QLabel 가 삭제된다면, label 변수는 무효한 주소 대신 nullptr 를 보유하게 되며, 마지막 줄은 절대 실행되지 않을 것입니다.

QPointer에서 사용할 수 있는 함수와 연산자는 일반적인 비보호 포인터에서 사용할 수 있는 것과 동일합니다. 단, 일반적으로 객체 배열에서만 사용되는 포인터 산술 연산자(+, -, ++, --)는 예외입니다.

QPointer를 일반 포인터처럼 사용하면 이 클래스 문서를 읽을 필요가 없습니다.

가드된 포인터를 생성하려면, T* 또는 동일한 유형의 다른 가드된 포인터로부터 생성하거나 할당할 수 있습니다. operator==() 및 operator!=(), 또는 isNull()을 사용하여 서로 비교하거나 nullptr 인지 확인할 수 있습니다. *x 또는 x->member 표기법을 사용하여 역참조를 수행할 수 있습니다.

가드된 포인터는 자동으로 T *로 형변환되므로, 가드된 포인터와 가드되지 않은 포인터를 자유롭게 혼합하여 사용할 수 있습니다. 즉, QPointer<QWidget>가 있다면, QWidget *를 요구하는 함수에 이를 전달할 수 있습니다. 이러한 이유로, QPointer를 매개변수로 받는 함수를 선언하는 것은 별다른 의미가 없으므로 일반 포인터를 사용하면 됩니다. 포인터를 장기간 저장해야 할 때에만 QPointer를 사용하십시오.

T 클래스는 반드시 QObject 을 상속받아야 하며, 그렇지 않으면 컴파일 또는 링크 오류가 발생합니다.

QSharedPointer, QObject 및 QObjectCleanupHandler도 참조하십시오 .

멤버 함수 설명서

[noexcept default] QPointer::QPointer()

[constexpr noexcept] QPointer::QPointer(std::nullptr_t)

nullptr 값을 가진 가드 포인터를 생성합니다.

isNull()도 참조하십시오 .

[noexcept, since 6.6] template <typename X> requires if_convertible<X> QPointer::QPointer(QPointer<X> &&other)

[noexcept, since 6.6] template <typename X> requires if_convertible<X> QPointer::QPointer(const QPointer<X> &other)

변환 생성자. ` other`에서 이동 또는 복사를 통해 새로운 ` QPointer ` 객체를 생성합니다.

이동 원본인 QPointer 는 nullptr로 재설정됩니다.

참고: 이 생성자들은 X* 가 T* 로 변환 가능한 경우에만 오버로드 해결에 참여합니다.

이 함수들은 Qt 6.6에서 도입되었습니다.

QPointer::QPointer(T *p)

p 가 가리키는 것과 동일한 객체를 가리키는 가드된 포인터를 생성합니다.

QPointer::~QPointer()

가드된 포인터를 소멸시킵니다. 일반 포인터와 마찬가지로, 가드된 포인터를 소멸시켜도 해당 포인터가 가리키는 객체는 소멸 되지 않습니다.

[noexcept] void QPointer::clear()

이 QPointer 객체를 지웁니다.

isNull()도 참조하십시오 .

[noexcept] T *QPointer::data() const

보호 대상인 객체에 대한 포인터를 반환합니다.

[noexcept, since 6.0] T *QPointer::get() const

data()와 동일합니다. 이 함수는 STL 호환성을 위해 제공됩니다.

이 함수는 Qt 6.0에서 도입되었습니다.

[noexcept] bool QPointer::isNull() const

참조된 객체가 소멸되었거나 참조된 객체가 없는 경우 ` true `를 반환하고, 그렇지 않은 경우 ` false`를 반환합니다.

[noexcept] void QPointer::swap(QPointer<T> &other)

이 포인터와 other 간의 위치를 바꿉니다. 이 작업은 매우 빠르며 절대 실패하지 않습니다.

[noexcept] QPointer::operator T *() const

형 변환 연산자; 포인터의 의미론을 구현합니다. 이 함수 덕분에 T*가 필요한 함수에 QPointer<T>를 전달할 수 있습니다.

[noexcept] T &QPointer::operator*() const

해참조 연산자; 포인터의 동작 방식을 구현합니다. 일반 C++ 포인터를 사용할 때와 마찬가지로 이 연산자를 사용하면 됩니다.

[noexcept] T *QPointer::operator->() const

오버로드된 화살표 연산자; 포인터의 동작 방식을 구현합니다. 이 연산자는 일반적인 C++ 포인터를 사용할 때와 마찬가지로 사용하기만 하면 됩니다.

[noexcept, since 6.6.1] template <typename X> requires if_convertible<X> QPointer<T> &QPointer::operator=(QPointer<X> &&other)

변환 이동 할당 연산자. 이 가드된 포인터가 ` other `가 가드하는 객체와 동일해지도록 하고, ` other `를 `nullptr`으로 재설정합니다.

제약 조건

X* 가 T* 로 변환 가능한 경우에만 오버로드 해결에 참여합니다.

이 함수는 Qt 6.6.1에서 도입되었습니다.

QPointer<T> &QPointer::operator=(T *p)

할당 연산자. 이제 이 가드된 포인터는 ` p `가 가리키는 것과 동일한 객체를 가리키게 됩니다.

[noexcept, since 6.6] template <typename X> requires if_convertible<X> QPointer<T> &QPointer::operator=(const QPointer<X> &other)

변환 할당 연산자. 이 가드된 포인터가 other 에 의해 가드되는 객체와 동일한 객체를 가드하도록 만듭니다.

제약 조건

X* 가 T* 로 변환 가능한 경우에만 오버로드 해결에 참여합니다.

이 함수는 Qt 6.6에서 도입되었습니다.

관련 비멤버 함수

[noexcept] template <typename X> bool operator!=(X *const &lhs, const QPointer<T> &rhs)

불등호 연산자. ` lhs `와 보호된 포인터 ` rhs `가 동일한 객체를 가리키지 않는 경우 ` true `를 반환하고, 그렇지 않은 경우 ` false`를 반환합니다.

[noexcept] template <typename X> bool operator!=(const QPointer<T> &lhs, X *const &rhs)

불등호 연산자. ` rhs `와 보호된 포인터 ` lhs `가 동일한 객체를 가리키지 않을 경우 ` true `를 반환하고, 그렇지 않으면 ` false`를 반환합니다.

[noexcept(...)] template <typename X> bool operator!=(const QPointer<T> &lhs, const QPointer<X> &rhs)

불등호 연산자. 보호된 포인터 lhs 와 rhs 가 동일한 객체를 가리키지 않는 경우 true 를 반환하고, 그렇지 않은 경우 false 를 반환합니다.

참고: true 이 true 인 경우,이 함수는 noexcept입니다.

[noexcept] bool operator!=(const QPointer<T> &lhs, const std::nullptr_t &rhs)

불등호 연산자. ` lhs `로 보호되는 포인터가 유효한(즉, ` nullptr`가 아닌) 포인터인 경우 ` true `를 반환하고, 그렇지 않은 경우 ` false`를 반환합니다.

[noexcept] bool operator!=(const std::nullptr_t &lhs, const QPointer<T> &rhs)

불등호 연산자. ` rhs `로 보호되는 포인터가 유효한(즉, ` nullptr`가 아닌) 포인터인 경우 ` true `를 반환하고, 그렇지 않은 경우 ` false`를 반환합니다.

[noexcept] template <typename X> bool operator==(X *const &lhs, const QPointer<T> &rhs)

등호 연산자. ` lhs `와 보호된 포인터 ` rhs `가 동일한 객체를 가리키면 ` true `를 반환하고, 그렇지 않으면 ` false`를 반환합니다.

[noexcept] template <typename X> bool operator==(const QPointer<T> &lhs, X *const &rhs)

등호 연산자. ` rhs `와 보호된 포인터 ` lhs `가 동일한 객체를 가리키는 경우 ` true `를 반환하고, 그렇지 않은 경우 ` false`를 반환합니다.

[noexcept(...)] template <typename X> bool operator==(const QPointer<T> &lhs, const QPointer<X> &rhs)

등호 연산자. 보호된 포인터 lhs 와 rhs 가 동일한 객체를 가리키면 true 를 반환하고, 그렇지 않으면 false 를 반환합니다.

참고: true 가 true 일 때이 함수는 noexcept입니다.

[noexcept] bool operator==(const QPointer<T> &lhs, const std::nullptr_t &rhs)

등호 연산자. ` lhs `로 보호되는 포인터가 ` nullptr`인 경우 ` true `를 반환하고, 그렇지 않은 경우 ` false`를 반환합니다.

[noexcept] bool operator==(const std::nullptr_t &lhs, const QPointer<T> &rhs)

등호 연산자. ` rhs `로 보호되는 포인터가 ` nullptr`인 경우 ` true `를 반환하고, 그렇지 않은 경우 ` false`를 반환합니다.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.