You are not logged in.

1

Friday, September 30th 2011, 3:36pm

QMessageBox question

Hello

Is there a problem if I write this:

Source code

1
QMessageBox::critical(NULL, title, message);


ie with the parent set to NULL.
Is this ok or it is a memory leak?

Thanks.

bst

Beginner

  • "bst" is male

Posts: 42

Location: Germany

  • Send private message

2

Friday, September 30th 2011, 9:03pm

Hi,

This is OK.

QMessageBox normally positions in the middle of the parent window. If you use 0 it will position in the middle of the screen.

cu, Bernd

3

Wednesday, October 5th 2011, 12:13pm

Thanks.

I thought that QMessageBox::critical may create a messagebox object on the heap (ie using "new") and that the parent needs to be specified so that the memory would be released after the messagebox has been closed.

bst

Beginner

  • "bst" is male

Posts: 42

Location: Germany

  • Send private message

4

Wednesday, October 5th 2011, 12:52pm

Hi,

no, the messagebox is created via:

Source code

1
QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);


in showNewMessageBox. See the source in qt-xyz/src/dialogs/qmessagebox.cpp. Parent is IMHO only used for modality:

Quoted

If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
{application modal} dialog box. If \a parent is a widget, the
message box is \l{Qt::WindowModal} {window modal} relative to \a
parent.

cu, Bernd

5

Tuesday, October 11th 2011, 9:22am

thanks :)

By the way, is there a quick method to find out which object you can allocate on the heap (ie via "new") without deleting, and which should be delete-d afterwards?
For instance, it is clear that the QString should be allocated on the stack, because it does not have as parameters a pointer to the parent. So, can all objects of all classes who have constructors that take as parameter a pointer to the parent be allocated on the heap (without the need to use delete afterwards)?

bst

Beginner

  • "bst" is male

Posts: 42

Location: Germany

  • Send private message

6

Thursday, October 13th 2011, 9:07am

Hi,

Qt's 'parent-child model' is implemented in QObject. The objects you have to delete yourself are the objects which are created with new and have no parent.

HTH, Bernd

Similar threads