tags:

views:

48

answers:

2

Hopefully this isn't too stupid but I want to make sure I'm doing this right.

Some Qt functions return Qt objects as values, but we may want to store them in a pointer somewhere. For example, in QDomDocument, the function documentElement returns a QDomElement, not a pointer to it. Now, as a member of my class I have:

QDomElement *listRootElement;

In a function that sets things up I am using this:

listRootElement = new QDomElement;
*listRootElement = mainIndex->documentElement();

(mainIndex is a QDomDocument.)

This seems to work, but I just want to make sure I'm doing it right and that nothing will come back to bite me.

It would be very similar for some of the image functions where a QPixmap might be returned, and I want to maintain pointers to QPixMap's.

Thanks for any comments!

A: 

Assuming that you want to store a pointer to a QDomElement for some reason, and assuming that you aware of the potential pitfalls with points (like, two pointers might point to the same object):

The only thing to keep in mind is that the popular 'parent takes care of deleting children' system which Qt uses is only available for QObject (sub-)classes. So when new'ing a QString or a QDomElement or something like that, keep in mind that you do have to delete it yourself, too.

Frerich Raabe
Good call I just added appropriate deletes.
Micah Yoder
A: 

I'm guessing, but I think this:

listRootElement = new QDomElement(mainIndex->documentElement());

...may allow the compiler to optimise better (see this question for some reasoning).

sje397
Ok. I *do* want to be able to edit the DOM and save it from elsewhere in the program. Is there a way to do this without creating the copy?
Micah Yoder
@Micah - I think I'm wrong about that. Usually, changing a copy doesn't affect the original - but these are just wrappers around an internal structure that *is* modified using the object's methods.
sje397