Obsolete Members for QWeakPointer

クラスQWeakPointer の以下のメンバは非推奨である。これらは古いソース・コードの動作を維持するために提供されています。新しいコードでは使用しないことを強くお勧めします。

パブリック関数

(deprecated) QWeakPointer(const QObject *other)
(deprecated) T *data() const
(deprecated) QWeakPointer<T> &operator=(const QObject *other)

メンバ関数ドキュメント

[deprecated] QWeakPointer::QWeakPointer(const QObject *other)

この関数は非推奨である。新しいコードでは使用しないことを強くお勧めします。

QObject other への弱い参照を直接保持する QWeakPointer を作成します。このコンストラクタは、T テンプレート型がQObject であるか、そこから派生している場合にのみ使用できます(そうでない場合はコンパイル・エラーになります)。

このコンストラクタは、QSharedPointer で作成されていなくても、どのQObject でも使用できます。

任意の QObject に対してこの方法で作成された QWeakPointer は、通常はQSharedPointer に昇格できないことに注意してください。

QSharedPointer およびQPointerも参照してください

[deprecated] T *QWeakPointer::data() const

この関数は非推奨である。新しいコードでは使用しないことを強く推奨する。

代わりにtoStrongRef() を使用し、返されたQSharedPointer に対して data() を使用する。

このQWeakPointer が追跡しているポインタの値を、それが削除されないことを保証せずに返します。この保証を得るには、QSharedPointer オブジェクトを返すtoStrongRef() を使用する。この関数が、ポインタがすでに削除されていると判断した場合、nullptr を返します。

ポインタの値を取得し、その値そのものを使用することは、例えばデバッグ文のように問題ありません:

    qDebug("Tracking %p", weakref.data());

しかし、ポインタの再参照が許されるのは、ポインタが削除されないことを外部的な手段で保証できる場合だけです。例えば、他のスレッドがポインターを削除することはできないし、あなたが呼び出す可能性のある関数も削除することはできないと確信できる場合です。

そのような場合は、以下のコードが有効です:

    // this pointer cannot be used in another thread
    // so other threads cannot delete it
    QWeakPointer<int> weakref = obtainReference();

    Object *obj = weakref.data();
    if (obj) {
        // if the pointer wasn't deleted yet, we know it can't get
        // deleted by our own code here nor the functions we call
        otherFunction(obj);
    }

この関数は注意して使用してください。

isNull() およびtoStrongRef()も参照の こと。

[deprecated] QWeakPointer<T> &QWeakPointer::operator=(const QObject *other)

この関数は非推奨である。新しいコードでは使用しないことを強くお勧めします。

このQWeakPointer に、QObject other への弱い参照を直接保持させます。この関数は、テンプレート型TQObject であるか、そこから派生している場合にのみ利用可能です。

QPointerも参照してください

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