Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Qt's parent-child relationship #17

Open
nurupo opened this issue Jul 19, 2014 · 1 comment
Open

Use Qt's parent-child relationship #17

nurupo opened this issue Jul 19, 2014 · 1 comment

Comments

@nurupo
Copy link

nurupo commented Jul 19, 2014

I see you write code like this:

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    MyWidget(QWidget *parent = 0) : QWidget(parent)
    {
        layout = new QVBoxLayout;
        label = new QLabel("Hello");
        layout->addWidget(label);
        setLayout(layout);
    }

    ~MyWidget()
    {
        delete layout;
        delete label;
    }

private:
    QVBoxLayout *layout;
    QLabel *label;
};

There is nothing particularly wrong with it, but there is more Qt-ish way of writing it.

In Qt, QObject and everything that derives from it (QWidget, QLabel, even your own class, etc), can take ownership of other QObjects through so called parent-child relationship, which is usually specified in the constructor or as a call to QObject::setParent.

For example, that's how you could reduce the code above if you would use this parent-child relation:

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    MyWidget(QWidget *parent = 0) : QWidget(parent)
    {
        QVBoxLayout *layout = new QVBoxLayout(this);
        QLabel *label = new QLabel("Hello", this);
        layout->addWidget(label);
    }
};

Note that we removed the destructor.
Here layout is told to be a child of MyWidget by passing this as parent argument to to QVBoxLayout's constructor QVBoxLayout::QVBoxLayout(QWidget * parent ). This means that when MyWidget is destroyed, it will also delete layout. The same is true for label.

Also, if you don't access layout and label variables from anywhere outside the constructor, you don't even have to make them class members.

Moreover, you don't need to call setLayout(layout), because by passing this to QVBoxLayout's constructor you already are specifying that layout is the layout of MyWidget.
From documentation for void QWidget::setLayout ( QLayout * layout ):

An alternative to calling this function is to pass this widget to the layout's constructor.

Here is a doc explaining parent-child relationship a bit more.

@nurupo nurupo changed the title Use Qt's parent-child relation Use Qt's parent-child relationship Jul 19, 2014
@Dman95 Dman95 self-assigned this Jul 20, 2014
@Dman95
Copy link
Owner

Dman95 commented Jul 20, 2014

Thank you! I'll refactor code according this in the future commits.

@Dman95 Dman95 removed their assignment Feb 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants