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<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)
T *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++ 포인터와 달리). TQObject 의 서브클래스여야 합니다.

가드 포인터는 다른 사람이 소유한 QObject 에 대한 포인터를 저장해야 할 때 유용하며, 따라서 해당 포인터에 대한 참조를 보유하고 있는 동안 소멸될 수 있습니다. 포인터의 유효성을 안전하게 테스트할 수 있습니다.

Qt 5에서는 QPointer를 사용할 때 동작에 약간의 변화가 생겼습니다.

  • QWidget (또는 QWidget 의 서브클래스)에서 QPointer를 사용할 때, 이전에는 QWidget 소멸자에 의해 QPointer가 지워졌습니다. 이제 Q포인터는 QObject 소멸자에 의해 지워집니다( QWeakPointer 객체가 지워질 때이므로). 위젯을 추적하는 모든 Q포인터는 QWidget 소멸자가 추적 중인 위젯의 자손을 소멸하기 전에는 지워지지 않습니다.

Qt는 또한 개별 포인터에 대한 참조 컬렉션을 유지하는 데 사용할 수 있는 참조 카운트 공유 포인터 객체의 구현인 QSharedPointer 를 제공합니다.

예제:

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

QLabel 가 그 사이에 삭제되면 label 변수는 잘못된 주소 대신 nullptr 를 보유하게 되며, 마지막 줄은 실행되지 않습니다.

Q포인터에서 사용할 수 있는 함수 및 연산자는 일반적으로 객체 배열에만 사용되는 포인터 산술 연산자(+, -, ++, --)를 제외하고는 일반 가드되지 않은 포인터에서 사용할 수 있는 함수와 연산자와 동일합니다.

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

가드 포인터를 생성하려면 T* 또는 동일한 유형의 다른 가드 포인터에서 생성하거나 할당할 수 있습니다. 연산자==() 및 연산자!=()를 사용하여 서로 비교하거나 isNull()를 사용하여 nullptr 를 테스트할 수 있습니다. *x 또는 x->member 표기법을 사용하여 참조를 해제할 수 있습니다.

가드 포인터는 자동으로 T *로 캐스팅되므로 가드 포인터와 가드되지 않은 포인터를 자유롭게 혼합할 수 있습니다. 즉, Q포인터<QWidget>가 있는 경우 QWidget *가 필요한 함수에 전달할 수 있습니다. 따라서 Q포인터를 매개변수로 받는 함수를 선언하는 것은 별 의미가 없으며, 그냥 일반 포인터를 사용하면 됩니다. 시간이 지남에 따라 포인터를 저장할 때는 QPointer를 사용하세요.

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

QSharedPointer, QObject, QObjectCleanupHandler참조하세요 .

멤버 함수 문서

[noexcept] QPointer::QPointer()

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

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

isNull()도 참조하세요 .

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

[noexcept, since 6.6] template <typename X, QPointer<T>::if_convertible<X> = true> 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] T *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, QPointer<T>::if_convertible<X> = true> 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, QPointer<T>::if_convertible<X> = true> 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)

부등식 연산자. 가드된 포인터 lhsrhs 가 같은 객체를 가리키지 않으면 true 을 반환하고, 그렇지 않으면 false 을 반환합니다.

참고: 이 함수는 truetrue 인 경우를 제외하고는 반환되지 않습니다.

[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)

등호 연산자. 가드된 포인터 lhsrhs 가 같은 객체를 가리키면 true 을 반환하고, 그렇지 않으면 false 을 반환합니다.

참고: 이 함수는 truetrue 인 경우를 제외하고는 반환되지 않습니다.

[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 을 반환합니다.

© 2025 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.