qt question infinite recursion?

Mar 11, 2018 at 6:32pm
hi guys so here is some basic setup code from QT but it is quite confusing

the problem is in the findStuff.cpp file we have a findStuff pointer named ui when initialise this inside the constructors ui(new Ui::findStuff), so shouldn't this cause an infinite recursion? because we are creating a new Ui::findStuff object in the constructors and by doing this in turn a new object of findStuff will be created with a findStuff pointer and yet again will initialise the findStuff pointer with a new findStuff object and this recursive chain should in theory keep going until the program crashes

I know there is different name spaces used but such as findStuff:: and Ui:: namespaces BUT they are still the same object we just declare the class findStuff to be in the Ui namespace,

how does this work and not crash?

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#ifndef FINDSTUFF_H
#define FINDSTUFF_H

#include <QWidget>

namespace Ui {
class findStuff;
}

class findStuff : public QWidget
{
    Q_OBJECT

public:
    explicit findStuff(QWidget *parent = 0);
    ~findStuff();

private slots:
    void on_searchButton_clicked();
    
private:
    Ui::findStuff *ui;
};

#endif // FINDSTUFF_H




findStuff.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include "findstuff.h"
#include "ui_findstuff.h"

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

findStuff::~findStuff()
{
    delete ui;
}

void findStuff::on_searchButton_clicked()
{
    
}
Mar 11, 2018 at 6:35pm
I know there is different name spaces used but such as findStuff:: and Ui:: namespaces BUT they are still the same object we just declare the class findStuff to be in the Ui namespace,


No they aren't.

https://ideone.com/Remkp9
Mar 11, 2018 at 7:10pm


No they aren't.

https://ideone.com/Remkp9




thanks Repeater,good example but isn't a bit different in this case?

findStuff is the class I named,it isn't an actual class in the QT library

so how are they different,
Mar 11, 2018 at 7:25pm
In QT, when you create a ui object, typically through the Ui designer, the final output that the compiler sees is a header file.

The header file contains a class, inside the Ui namespace. That's one class.

You have another class, typically with the same name, NOT inside the Ui namespace. That's another, different class.

Two classes. Same name. Inside different namespaces.
Mar 11, 2018 at 7:27pm
No, the example is practically identical.

An identifier defined in namespace Ui has no relation to any identifier defined in global namespace. That is the whole point of namespaces. Each namespace can reuse names.

::findStuff != Ui::findStuff


The class Ui::findStuff is not defined by you. Not directly. The 'uic' (UI Compiler) writes that class definition from XML-file (findStuff.ui ?) that you did save from Qt Designer (or was it Creator?). You did not write the 'ui_findstuff.h' yourself, did you?
Last edited on Mar 11, 2018 at 7:31pm
Mar 11, 2018 at 7:58pm


The class Ui::findStuff is not defined by you. Not directly. The 'uic' (UI Compiler) writes that class definition from XML-file (findStuff.ui ?) that you did save from Qt Designer (or was it Creator?). You did not write the 'ui_findstuff.h' yourself, did you?




oh so in laymans terms QT behind the scenes creates a UI::findStuff behind the scenes for you?


You did not write the 'ui_findstuff.h' yourself, did you?


nope didn't write that,the ui_findstuff.h doesn't seem to appear in the headers folder,is there anyway I can view it?

thanks
Last edited on Mar 11, 2018 at 8:02pm
Mar 11, 2018 at 8:20pm
is there anyway I can view it?


It exists as a file, on your hard drive. Find it, view it. Qt probably puts it in a temporary or build directory somewhere.
Mar 11, 2018 at 8:27pm
... and you have to compile at least once, for building the project creates that file.
Mar 11, 2018 at 9:06pm

I'll have a look :)


and you have to compile at least once, for building the project creates that file.


got ya :) thanks guys
Mar 11, 2018 at 9:14pm
found it here is ui_findstuff.h


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

/********************************************************************************
** Form generated from reading UI file 'findstuff.ui'
**
** Created by: Qt User Interface Compiler version 5.10.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_FINDSTUFF_H
#define UI_FINDSTUFF_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_findStuff
{
public:
    QVBoxLayout *verticalLayout;
    QTextEdit *textEdit;
    QGridLayout *gridLayout;
    QLabel *label;
    QLineEdit *lineEdit;
    QPushButton *searchButton;

    void setupUi(QWidget *findStuff)
    {
        if (findStuff->objectName().isEmpty())
            findStuff->setObjectName(QStringLiteral("findStuff"));
        findStuff->resize(400, 300);
        verticalLayout = new QVBoxLayout(findStuff);
        verticalLayout->setSpacing(6);
        verticalLayout->setContentsMargins(11, 11, 11, 11);
        verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
        textEdit = new QTextEdit(findStuff);
        textEdit->setObjectName(QStringLiteral("textEdit"));

        verticalLayout->addWidget(textEdit);

        gridLayout = new QGridLayout();
        gridLayout->setSpacing(6);
        gridLayout->setObjectName(QStringLiteral("gridLayout"));
        label = new QLabel(findStuff);
        label->setObjectName(QStringLiteral("label"));

        gridLayout->addWidget(label, 0, 0, 1, 1);

        lineEdit = new QLineEdit(findStuff);
        lineEdit->setObjectName(QStringLiteral("lineEdit"));

        gridLayout->addWidget(lineEdit, 0, 1, 1, 1);

        searchButton = new QPushButton(findStuff);
        searchButton->setObjectName(QStringLiteral("searchButton"));

        gridLayout->addWidget(searchButton, 0, 2, 1, 1);


        verticalLayout->addLayout(gridLayout);


        retranslateUi(findStuff);

        QMetaObject::connectSlotsByName(findStuff);
    } // setupUi

    void retranslateUi(QWidget *findStuff)
    {
        findStuff->setWindowTitle(QApplication::translate("findStuff", "findStuff", nullptr));
        label->setText(QApplication::translate("findStuff", "search", nullptr));
        searchButton->setText(QApplication::translate("findStuff", "Search", nullptr));
    } // retranslateUi

};

namespace Ui {
    class findStuff: public Ui_findStuff {}; // why do we add this namespace at the end of the file
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_FINDSTUFF_H



nothing too complicated,makes sense

thanks
Last edited on Mar 11, 2018 at 9:15pm
Topic archived. No new replies allowed.