Search code examples
qtconstructorqt4

I'm following Qt Tutorials and got a simple question


If I want to create my own class MyWidget which inherits from QWidget

Tutorial tells me to write constructor like this...

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent){....}

I'm wondering what is the role of : QWidget(parent)

Does it mean explicit call for QWidget's constructor?


Solution

  • Yes. In C++, if you must explicitly call the base class's constructor in your constructor's initialization list if you want it to run. In this case, QWidget(parent) would be run before running the code in your constructor. By the way, this is not just a Qt thing, but is common in C++ inheritance.