Greetings. This is more of a C++ question, but I'm posting it here because I would like to know how it relates to Qt.
Reading books on Qt, I know that widgets should be constructed on the heap because they will be deleted automatically when their parents are deleted. My question is this: if I have a custom widget class and I instantiate it on the heap, would the members also go on the heap even though they are not dynamically created in the class?
private:
};
...
MyWidget *w = new MyWidget;
class MyWidget : public QWidget{
private:
QPushButton myButton_;
};
...
MyWidget *w = new MyWidget;
To copy to clipboard, switch view to plain text mode
In the example code does 'myButton_' reside on the heap or the stack? Printing the memory address it seems that it's on the heap. Does this mean that I can avoid using the 'new' operator and dynamic memory allocation for members of my class if I know for sure that the class will be constructed on the heap?
or is it always best to use pointers regardless?
public:
MyWidget(){
}
~MyWidget(){
delete pMyButton_;
}
private:
};
...
MyWidget *w = new MyWidget;
class MyWidget : public QWidget{
public:
MyWidget(){
pMyButton_ = new QPushButton;
}
~MyWidget(){
delete pMyButton_;
}
private:
QPushButton *pMyButton_;
};
...
MyWidget *w = new MyWidget;
To copy to clipboard, switch view to plain text mode
Bookmarks