Requesting an instance of a Qt object

QObject *obj;
...
if ( /* obj is already instantiated */ ) {
    ;
} else {
    obj = new QObject();
}

      

My query is an if condition

+3


source to share


3 answers


1) Initialize a pointer to a NULL object

2) Check for null value in statement if



QObject *obj = NULL;
...
if ( obj != NULL ) {
    ;
} else {
    obj = new QObject();
}

      

+5


source


QObject *obj = 0;

// ...

if (!obj) obj = new QObject();

      




Note. Does not guarantee that obj

it is not a dangling pointer .

0


source


Use the class QPointer

. http://qt-project.org/doc/qt-4.8/qpointer.html#data

Whenever you call deleteLater();

it sets the value to 0 than you can check.

QPointer<QWidget> someWidget = 0;

0


source







All Articles