Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A newbie question in Qt

Tags:

c++

qt

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

In this section of code, after : what does

QMainWindow(parent),
ui(new Ui::MainWindow)  

mean?

like image 237
Sean Avatar asked Jan 31 '11 02:01

Sean


3 Answers

This is actually a C++ question. What you're looking at are called initialization lists. The QMainWindow(parent) calls that constructor for MainWindow's superclass (QMainWindow) and ui(new Ui::MainWindow) constructs ui member.

like image 133
Travis Gockel Avatar answered Nov 08 '22 19:11

Travis Gockel


By way of further explanation, observe that the class generated by the user interface compiler is also called (in this case) MainWindow, but it belongs to the namespace Ui, so its full name is Ui::MainWindow. This is emphatically not the same as the MainWindow class that inherits from QMainWindow. However, the QMainWindow-derived class has a member pointer (called ui) to a Ui::MainWindow, which is initialized in the constructor, as explained above. Have a look at the uic-generated file ui_mainwindow.h to see how all the pieces fit together. Note also that the members of Ui::MainWindow are all public and therefore fully accessible within MainWindow.

An alternative design is to merge the identity of the two MainWindow classes using multiple inheritance, deriving your own MainWindow from both QMainWindow and Ui::MainWindow. However, the code generated by current version of QtCreator follows the composition pattern rather than the inheritance pattern.

like image 21
Jan Hettich Avatar answered Nov 08 '22 18:11

Jan Hettich


These two lines are the so-called initialization list and are executed at "creation" time of each instance of this class. Each class inheriting another should contain a call to the superclass' constructor in this list.

You could also write:

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

which one could find better readable. But using an initialization list is slightly faster and is being optimized by the compiler. Note that these lists can only be used in constructors and you cannot call any functions of the object - because it doesn't "live" yet. But you may set the value of some attributes and refer to them in following statements (to avoid code redundancy for example), like in the following example:

#define DEFAULT_VALUE 1.0

class MyClass {

public:
    MyClass() :
        value1(DEFAULT_VALUE),
        value2(value1)
    {
    }

    MyClass(qreal value) :
        value1(value),
        value2(value1)
    {
    }

private:
    qreal value1;
    qreal value2;
};

Note that most compilers give you a warning if the order of the members in your initialization list doesn't match the order in the class definition.

like image 5
leemes Avatar answered Nov 08 '22 20:11

leemes