#include#include #include class MyMainWindow : public QMainWindow { Q_OBJECT public: explicit MyMainWindow(QWidget* parent = nullptr); private: QPointer m_button; }; MyMainWindow::MyMainWindow(QWidget* parent) : QMainWindow(parent) { m_button = new QPushButton("Close", this); connect(m_button, &QPushButton::clicked, this, &QMainWindow::close); }
#includeIn this example, we have a MyClass object that is managed by the QPointer. We then call deleteLater() on the object. This function posts an event to the object's thread's event loop and the object will be deleted when the event is processed. And finally, we check if the pointer is null or not. This code uses the QtCore package library from the Qt library.#include #include class MyClass : public QObject { public: MyClass() { qDebug() << "MyClass Constructor"; } ~MyClass() { qDebug() << "MyClass Destructor"; } }; int main() { QPointer myObj = new MyClass; myObj->deleteLater(); qDebug() << "Checking if pointer is null: " << (myObj.isNull() ? "Yes" : "No"); return 0; }