Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

class Wizard : public QDialog

    Wizard(QWidget *parent);

    private slots:
    void what_ever();

    QPushButton *what_ever;  

Wizard::Wizard( QWidget *parent ) : QDialog(parent)
  QGridLayout *layout = new QGridLayout( this );

  QScopedPointer<QTextEdit> textEdit(new QTextEdit); 
  layout->addWidget( textEdit.take(), 0, 0, 1, 2 );

  what_ever = new QPushButton("whatever");
  layout->addWidget(what_ever, 0, 1, 1, 1);

  connect(what_ever, SIGNAL(clicked()), this, SLOT(what_ever()));

void Wizard::what_ever()
  //blah blah blah

I have some problems about the codes.

1 : What if textEdit throw exception?
If "textEdit" throw exception, that means the
destructor of Wizard would not be called,
What would Qt handle the resource of "layout"?

2 : Could I initialize "what_ever" like this?
layout->addWidget(what_ever = new QPushButton("whatever"), 0, 1, 1, 1);
Is this safe in Qt4(4.8)?Would it have any change to cause memory leak?

Thanks a lot

I've been working extensively with Python and Qt via the PyQt bindings from Riverfront.

First of all, an "object" doesn't throw an exception, code associated with the object does. So what you may want to encapsulate in a try/catch block is any method call on the textEdit. The constructor almost certainly won't throw an exception, so no need to worry about it there.

For your second question, both forms are equivalent, and the layout becomes the owner of the widget. The widget is destroyed when the layout is destroyed, there shoud be no memory-leak as a result. Memory leaks in Qt come about when a widget for some reason needs to keep track of its parent, and then the grandparent releases the parent. Since the parent and child still refer to each other, they can get stranded. So just watch out for unintended cycles in your referencing.

This article has been dead for over six months. Start a new discussion instead.