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

Remove unnecessary manual destruction and use local variables in the constructor instead of class memebers #541

Open
coder3101 opened this issue Aug 1, 2020 · 1 comment

Comments

@coder3101
Copy link
Member

@coder3101 coder3101 commented Aug 1, 2020

There are two sets of refactoring that can be performed on our codebase to make them more efficient and utilize the full potential offered by Qt Framework. See below:

  1. There is no need to explicitly call delete on widgets that are allocated on Heap as long as in their constructor we have specified the parent widget for it. Consider this example below:
class Widget: public QObject
{
Q_OBJECT
public:
   Widget(){ view = new MyAwesomeWidget(this); /* this is pointer to parent widget and is required */ }
  ~Widget(){ delete view; /* delete is not required, Qt will automatically delete it */ }
};
  1. We can also make many widgets local to the constructor instead of making them member variables. Consider this example below:
class Widget: public QObject
{
Q_OBJECT
    Widget(){
       auto layout = QHVBoxLayout(this);
       // layout is a member function in the current codebase but we want to make it local
       // because layout is not required to be referenced anywhere else after we have added it
       // unlike button widget which is required by chageButtonText function and hence must be a member function.
       // Your task will be to find all such widgets and make them local to constructor. This step should be ideally done after 
       // first point. 
       button = QPushButton();
       layout->addWidget(button);
     }
    void changeButtonText(QString foo){
        button->setText(foo);
    }
private:
    QPushButton* button = nullptr;
};

Describe the solution you'd like

As described above

Describe alternatives you've considered

N/A

Additional context

See #539 (review)

@ouuan
Copy link
Member

@ouuan ouuan commented Aug 1, 2020

In addition to this:

  1. We don't need to set the parent of an item if it's added to a layout. And we shouldn't set the parents of more than one layout to the same widget, because a layout is considered as its parent's layout, and a widget can only have one layout.
  2. Sometimes, we should care about the order of destruction. For example, in AppWindow SettingsManager::deinit() should be called at last, and in MainWindow log should be last destructed. In these cases, the destructor could be necessary, or we should set the parents carefully.
@ouuan ouuan changed the title Remove delete* widget from destructor by making local constructor object instead of class members. Remove unnecessary manual destruction and use local variables in the constructor instead of class memebers Aug 1, 2020
@ouuan ouuan mentioned this issue Aug 2, 2020
9 of 9 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.