I have recently began working with the Qt framework and realized there may be a bit about the syntax of C++ that I do not quite understand. Take for example this code that is given as a foundation when starting a Widget project in Qt.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
Where the class MainWindow
is defined as:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
Class declaration
Stepping through this, I can see that a new namespace,
Ui
, is created. I imagine this to be an implementation effort to avoid naming conflicts with other user-implemented classes.A new class definition follows; it inherits
QMainWindow
, so we are essentially creating a custom window object.Q_OBJECT
is apparently a macro that allows the user to use signals and slots in the class definitions, but I am not too worried about that code right now.We define the constructor to be
explicit
thereby disallowing any implicit conversions of this class type. There is also a destructor shown.In the private accesses, we look to the
Ui
namespace and create aMainWindow
pointer.
Class definition
We are using an initialization list I see, but I get lost here. I see that we have a default parameter of NULL ( or 0 ) for the parent of this widget, but if a class inherents another class why must you explicitly call the constructor for said inherited class? That is:
class A { } class B : public A { } B testObject;
Does this not automatically allocate memory on the stack sufficient memory to contain both classes A and B? Why does the Qt code shown above call the constructor for an inherited class within its own constructor?
Continuing to the second parameter of the initialization list, I must say that I am confused on the syntax used here. Why do we have a pointer to a
QMainWindow
that we initialize by callingQMainWindow
's constructor with a further call tonew
thereby creating an instance of itself? This initialization method seems circular.We then call a setupUi function and pass
this
object for initialization.And finally, in the destructor we simply deallocate the memory associated with the
MainWindow
.
As a final question, if an additional object is created, say a QPushButton
, does the software automatically deallocate the memory assocated with these objects or must that be included in the destructor alongside delete ui
? I have not seen a tutorial that actually does this, so I am not sure if the framework is designed such that it is handled for you.
I know there are few direct questions here, but is there any flaw in my logic for each statement?
tl;dr, skip to the first and second bullet under Class definition.