qt setup,infinite recursion?



  • hello peeps 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

    #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

    #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()
    {
        
    }
    

  • Qt Champions 2017

    @adamchalkley2018 said in qt setup,infinite recursion?:

    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,

    And because they are in different namespaces they are indeed different classes. Qt is just C++ and cannot overcome the restrictions of C++. If you look in the ui_findstuff.h you will see how the UI is implemented. This file is created from the findstuff.ui (a XML file that can be edited with Qt Designer) by uic and afterward compiled by the C++ compiler.



  • thanks for the reply aha

    but how come when I try to do this in my own project it won't work I pretty much get a forward declaration error yet you can do it in the QT code without getting an error

    #ifndef TEST_H
    #define TEST_H
    
    #include <iostream>
    
    
    class Test
    {
        public:
            Test();
            void print();
            virtual ~Test();
    
        protected:
    
        private:
    
    };
    
    #endif // TEST_H
    
    #include "Test.h"
    
    Test::Test()
    {
        //ctor
    }
    
    void Test::print(){
    
      std::cout << "hey";
    }
    
    Test::~Test()
    {
        //dtor
    }
    
    #define TESTTWO_H
    
    #include "Test.h"
    
    namespace Ui{
    
     class testTwo;
    }
    
    
    
    class testTwo : public Test
    {
        public:
            testTwo();
            virtual ~testTwo();
    
        protected:
    
        private:
            testTwo *ui;
    };
    
    #endif // TESTTWO_H
    
    #include "testTwo.h"
    
    testTwo::testTwo()
    : Test(),ui(new Ui::testTwo()) // error on this line 
    {
        //ctor
    }
    
    testTwo::~testTwo()
    {
        //dtor
    }
    

  • Qt Champions 2017

    @adamchalkley2018

    I guess that is because your code is missing the #include ui_test2.h line.

    As said, have a look in ui_findstuff.h and you will be surprised. No magic involved here.



  • thanks aha found it,that makes sense,I'm surprised how QT creates a class for you it's pretty awesome but does this require an extra step during compilation ?


  • Lifetime Qt Champion

    Hi,

    uic is run.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.