简体   繁体   中英

Why do we pass “this” pointer to setupUi function?

I'm fairly new in QT. Taking below fairly simply explain from qt docs :

class CalculatorForm : public QWidget
 {
     Q_OBJECT
 public:
     CalculatorForm(QWidget *parent = 0);
 private slots:
     void on_inputSpinBox1_valueChanged(int value); //why is that slots are private?
 private:
     Ui::CalculatorForm ui;
 };

and implementation of constructor

 CalculatorForm::CalculatorForm(QWidget *parent)
     : QWidget(parent) {
     ui.setupUi(this); // <-- Question below
 }

Q: I was wondering why do we pass this pointer to setupUi function?, what does it do ?

So that the dialog will have the caller as parent, so that eg when the parent is closed the dialog can be closed automatically. Generally all gui elements have a pointer to their parent.

 private slots:
     void on_inputSpinBox1_valueChanged(int value); //why is that slots are private?

These are auto generated slots which exactly match the naming of the gui elments in QtDesigner. They are only meant to do the direct hookup to those gui elements and so should be dealt with in this class. If these signals were extended to other classes then any change in the gui would require changing a lot of other code which doesn't need to know details of the gui.

In the handler slot for the specific gui element you can then emit another more general signal to the rest of the app.

The only widget that setupUi doesn't create is the widget at the top of the hierarchy in the ui file, and as the Ui::CalculatorForm class instance doesn't know the widget it has to fill, it ( this ) has to be passed explicitly to the class at some point.

this or any other widget you would pass to it, is used as the parent to all other subwidgets. For example, you could fill a widget without inheritance like this:

QWidget *widget = new QWidget;
Ui::CalculatorForm *ui = new Ui::CalculatorForm;
ui->setupUi(widget);
widget->show();

But really, it would be easier to understand if you read the content of the uic generated file (probably named ui_calculatorform.h ).

setupUi creates the instances of widgets (QLabel, QTextEdit and so on). The [user interface compiler] (http://qt-project.org/doc/qt-4.8/uic.html) gets information for you from the .UI form and generates widget-creation code in the generated moc source files.

The manual way of creating widgets without using the Qt Designer or a UI file would be like so:

QWidget* pWidget = new QWidget(this);

I think it is to add the caller widget to the layout of this UI. This widget will be the toplevel widget.

Martin Beckett answer might be correct as well, as what he described is a common behavior in Qt (cf the 'parent' argument in most of widget's derived class constructor)

Note that you have alternative ways how designer can auto-generate code. In this case you have a separate 'UI' class for this code which is not QObject so it also is not a QWidget. Auto generated code needs information about parent widget and to make auto-conections of slots and signals so this is why you have to pass this .
This pater is less intrusive then other pasterns (that is why it is default). You can also try alternative patters (check Qt Creator Designer options), but I recommend you to see what is generated by designer tools in default settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM