开发者

QWidget deletion problem

I have a standard QWidget-derived class, but I get double frees upon widget destruction. whenever I add a QTreeView pointer as a member variable.

E.g.:

private:
    QTreeView *m_treeView;

In the class's constructor, I do a simple:

m_treeView = new QTreeView(this);

And the QWidget-derived class's destructor is the default destructor.

If I forgo using a member pointer entirely and do:

QTreeView *treeView = new QTreeView(this);

Everything is fine. Having QLabel member pointers also works fine. Why am I seeing this behavior? Whether or not the pointer is a m开发者_如何学Pythonember of the class should have no bearing on the class's destructor since the objects are being created on the heap, and classes don't delete the objects that their member variables point to unless instructed to do so in a custom destructor.


Whenever your QWidget-derived object is deleted, m_treeView will also be deleted since you passed this as parent object when you constructed m_treeView

This happens because new QTreeView(this) will eventually call QObject( QObject * parent ) and that means your member variable gets added as a child object of your QWidget-derived object.

The destructor of a parent object destroys all child objects.

From the Qt docs

EDIT: Sorry, missed the

And the QWidget-derived class's destructor is the default destructor.

part

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜