I don't think the issue is with how you instantiate the QWidget. The way Qt works when one object is deleted all its children get deleted. The difference here is just when the QWidget will get deleted - when you use the "new" the object will only get deleted when the application exits and the OS reclaims the memory.

My guess it that you will get the same error using the pointer if you add after line 20 a
@
delete window;
@

Most likely the issue is with Qt trying to delete the "textedit" or "layout" objects when the QWidget gets deleted.

Please note that is not good practice to leave for the OS to reclaim memory even if it seems to work.

This is a paragraph from the "C++ GUI Programming with Qt 4 - 1st edition by Jasmin Blanchette and Mark Summerfield (page 28):

bq. Qt’s parent–child mechanism is implemented in QObject. When we create an object (a widget, validator, or any other kind) with a parent, the parent adds the object to the list of its children. When the parent is deleted, it walks through its list of children and deletes each child. The children themselves then delete all of their children, and so on recursively until none remain.

This is a very good book and the first edition is available as a pdf for free. Look into the "Qt books" wiki.

// 1.If I use the object,I will get an error whe I quit the application.
// Debug it,I get a signal SIGSEGV which means Segmentation fault
QWidget window;
window.setLayout(&layout);
window.show();
// 2.But if I use the poniter ,it is ok.
// QWidget *window=new QWidget;
// window->setLayout(&layout);
// window->show();
return app.exec();
}
[/quote]

You really need to learn C++ and figure out why the two ways of doing things are different.

First of all, as people already said, you're implicitly using QObject parent/children mechanism, which means that destroying an object will automatically destroy all of its children.

Second, what's going on here is mandated by the C++ standard: automatic object destruction happens in the reverse order than automatic object construction, that is, the last object created is the one that is destroyed first.

What's the last object that was built? The "QWidget window". And its dtor wipes out (with operator delete) all its children, namely the layout and the textedit, which were allocated on the stack. Uh oh... operator delete on an object on the stack. That's WRONG!

What happens then? The dtors of the layout and of the textedit objects are run (WRONG! and WRONG! again, or, WRONG²!). Well, it's too late. Your program's memory layout is already FUBAR, and you've probably already got a weird crash by now.

If you use the @QWidget window = new QWidget@ approach instead your application will leak memory (WRONG!*) and tools like valgrind will complain and report: you never free the memory allocated there.

Of course, adding a @delete window@ (GOOD!) line after app.exec() will crash your application for the same reason stated above (WRONG!).

So, what you can do about that? Either

allocate everything on the heap with operator new, then delete ONLY the widget with operator delete;

// 1.If I use the object,I will get an error whe I quit the application.
// Debug it,I get a signal SIGSEGV which means Segmentation fault
QWidget window;
window.setLayout(&layout);
window.show();
// 2.But if I use the poniter ,it is ok.
// QWidget *window=new QWidget;
// window->setLayout(&layout);
// window->show();
return app.exec();
}
[/quote]

You really need to learn C++ and figure out why the two ways of doing things are different.

First of all, as people already said, you're implicitly using QObject parent/children mechanism, which means that destroying an object will automatically destroy all of its children.

Second, what's going on here is mandated by the C++ standard: automatic object destruction happens in the reverse order than automatic object construction, that is, the last object created is the one that is destroyed first.

What's the last object that was built? The "QWidget window". And its dtor wipes out (with operator delete) all its children, namely the layout and the textedit, which were allocated on the stack. Uh oh... operator delete on an object on the stack. That's WRONG!

What happens then? The dtors of the layout and of the textedit objects are run (WRONG! and WRONG! again, or, WRONG²!). Well, it's too late. Your program's memory layout is already FUBAR, and you've probably already got a weird crash by now.

If you use the @QWidget window = new QWidget@ approach instead your application will leak memory (WRONG!*) and tools like valgrind will complain and report: you never free the memory allocated there.

Of course, adding a @delete window@ (GOOD!) line after app.exec() will crash your application for the same reason stated above (WRONG!).

So, what you can do about that? Either

allocate everything on the heap with operator new, then delete ONLY the widget with operator delete;

The top level widget can be created on the stack, so it is then deleted automatically, or you can create it on the heap and use a smart pointer (scoped ptr) to automatically delete it. But child widgets MUST be created on the heap.

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)