How does the class constructor statement in Qt work?

Asked

Viewed 711 times

7

I work with C and a little bit of Assembly, in Atmel AVR microcontrollers. I’m trying to understand how the framework Qt 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 in the first row after the operator :? is some kind of inheritance? to my look like a kind of boot, there could be this after the operator :?

[2] In the construction of the object in the archive _main.cpp_, 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 more complete way, I thank you.

1 answer

7


You understood right, it has to do with inheritance, at least in the first case. This is a startup list. In this case the manufacturer MainWindow is calling the builder of QMainWindow, obviously passing the parameter received as argument to this constructor. This has to do with the way the argument needs to be passed that otherwise is not so intuitive. And if I’m not mistaken about the boot order, but I may be confusing languages.

He also rides a member called ui Initiating with a class MainWindow existing in the namesapace Ui. This is already a composition. Note that it does this before starting to run the actual code within the constructor. Although the second has no advantages of being there. I could write like this:

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

I put in the Github for future reference.

On the second question, there is no argument, w will be initialized with a constructor with no arguments that must also exist in the class or it is initializing the window with a value default, as Luiz Vieira said in the comments below. The main window has no parent control, no need to pass anything to it or pass a null value indicating the absence of a parent window. This can be obtained with another language resource that the parameter assumes a value in the absence of an argument. Then a constructor with no arguments, as shown above could be calling a constructor with parameters, as long as there is a value default established in them.

  • very enlightening and objective his answer. The reference left was a perfect complement to exemplify the content in his reply. thank you very much.

  • In fact, and if I’m not mistaken, the parameter parent main window constructor in Qt is usually declared with a default equal to 0 (null), following what the implementation of QMainWindow ago. :)

  • 1

    @Luizvieira may be, I don’t remember this but it makes sense.

  • Very clear. It helped a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.