Dewsworld
Dewsworld

Reputation: 14033

Qt Object instantiation query

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

My query is the condition of the if

Upvotes: 3

Views: 3807

Answers (3)

Rafael Oliveira
Rafael Oliveira

Reputation: 2923

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

Whenever you call deleteLater(); it will set it to 0, than you can check it.

QPointer<QWidget> someWidget = 0;

Upvotes: 0

Samuel Harmer
Samuel Harmer

Reputation: 4412

QObject *obj = 0;

// ...

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

Note: Makes no guarantee obj is not a dangling pointer.

Upvotes: 0

Chris
Chris

Reputation: 17535

1) Initialize your object pointer to NULL

2) Check for NULL in your if statement

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

Upvotes: 5

Related Questions