Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qt setup,infinite recursion?
QtWS25 Last Chance

qt setup,infinite recursion?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adamchalkley2018
    wrote on last edited by adamchalkley2018
    #1

    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()
    {
        
    }
    
    aha_1980A 1 Reply Last reply
    0
    • A adamchalkley2018

      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()
      {
          
      }
      
      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adamchalkley2018
        wrote on last edited by
        #3

        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
        }
        
        aha_1980A 1 Reply Last reply
        0
        • A adamchalkley2018

          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
          }
          
          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • A Offline
            A Offline
            adamchalkley2018
            wrote on last edited by
            #5

            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 ?

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              uic is run.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved