How does the class constructor declaration in Qt?

7

Work with C and a little assembly on Atmel AVR microcontrollers. I'm trying to understand how the framework extends C ++.

I created a new project with Qt Creator (Widgets), and generated the following code:

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), //<-- qual a relação do ponteiro acima e o parâmetro passado aqui?
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

[1] What is happening on the first line after the : operator? Is it some kind of inheritance? to my look it seems like a type of initialization, could there be this after the : ? operator

[2] In constructing the object in the _main.cpp_ file, where are the constructor arguments?

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w; //<-- Onde estão os argumentos?
    w.show();

    return a.exec();
}

If it can be explained, in a slightly more complete way, I thank you.

    
asked by anonymous 08.08.2015 / 22:44

1 answer

7

You got it right, it has to do with inheritance, at least in the first case. This is a bootlist . In case the MainWindow constructor is calling the QMainWindow constructor, obviously passing what it received as parameter to this constructor. This has to do with the way the argument needs to be passed which otherwise is not so intuitive. And if I am not mistaken with the boot order, but I may be confusing languages.

It also mounts a member called ui by initializing with a MainWindow class in namesapace Ui . This is already a composition. Note that it does this before you start running the actual code inside the constructor. Although the second has no advantages to be there. You could write like this:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {  
    ui = new Ui::MainWindow();
    ui->setupUi(this);  
}

On the second question, there is no argument whatsoever, w will be initialized with a constructor without arguments that must also exist in the class or it is initializing the window with a default value, as the Luiz Vieira in the comments below. The main window does not have a parent control, you do not have to pass anything to it or pass a null value indicating the absence of a parent window. This can be obtained with another language feature as the parameter assumes a value in the absence of an argument. So a constructor with no arguments like the one shown above could be calling a constructor with parameters, as long as there is a default value set in them.     

08.08.2015 / 23:27